简体   繁体   中英

Unable to set TextView programatically

I want to show a list of three separate textviews in my linear layout. I'm aware that this can be done from the XML file itself, but I want to generate these textviews dynamically.

XML file:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:card_view="http://schemas.android.com/apk/res-auto" 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
 android:id = "@+id/ll1">
    </LinearLayout>

Java file:

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;

import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RegistrationActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle saved){
        LinearLayout ll = findViewById(R.id.ll1);
        super.onCreate(saved);
        setContentView(R.layout.activity_reg);
        List<String> names = new ArrayList<>(Arrays.asList("Negotiation","Pyschology","Joke"));
        for (int i = 0; i<3;++i){
            TextView dynamic = new TextView(this);
            dynamic.setText(names.get(i));
            dynamic.setTextSize(14);
            dynamic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(RegistrationActivity.this, MainActivity.class));

                }
            });
            ll.addView(dynamic);


        }

    }
}

The result is a blank page and what I want is three clickable textviews as the result. Where did I go wrong?

If I am not wrong sir you want to add Text view Programmatically

Find you need a layout parent layout Your parent layout is Linear layout set id of a linear layout Suppose id is linerlayout_parent After this in Activity

LinearLayout linearview =  findViewById(R.id.linerlayout_parent);

Now Here Create text view Programatically

        TextView title=new TextView(Activity.this);
       title.setTextSize(20);
       title.setTextAppearance(context,R.font.abhaya_libre_bold);
       title.setPadding(10,10,10,10);
       title.setTextColor(Color.BLACK);
       title.setText("Hello use this code");
       linearview.addView(title);

if you need three than in Loop paste this code Kindly set orientation of a Linear layout Vertical

May be you forgot to add textview in parentview like this.

  tv[i] = new TextView(this); 
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
     ((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
params.leftMargin = 50;
params.topMargin  = i*50;
tv[i].setText(str[i]);
tv[i].setTextSize((float) 20);
tv[i].setPadding(20, 50, 20, 50);
tv[i].setLayoutParams(params);
rl.addView(tv[i]);  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM