简体   繁体   中英

Displaying textview below another textview programically using existing textview design

I have a Textview design in activity_main.xml which I want to use for displaying one textview below another. I got this code from stackoverlow itself and made a little change to use the existing Textview design. But the app closes when opened displaying nothing. I'm very new to Android Development.

XML code for TextView:

<TextView
    android:id="@+id/textviews"
    android:layout_width="match_parent"
    android:layout_height="104dp"
    android:textSize="30sp" />

Here is the code I got from Stackoverflow:

String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);        
for( int i = 0; i < textArray.length; i++ )
{
TextView textView = new TextView(this);
textView.setText(textArray[i]);
linearLayout.addView(textView);
}

This code runs perfectly with no issues. But when I tried calling the existing textview design by using findViewById, the code builds but the app never opens. Here it is:

String[] textArray = {"One", "Two", "Three", "Four"};
    LinearLayout linearLayout = new LinearLayout(this);
    setContentView(linearLayout);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    for( int i = 0; i < textArray.length; i++ )
    {
        TextView textView =(TextView) findViewById(R.id.textviews);
        textView.setText(textArray[i]);
        linearLayout.addView(textView);
    }

How do I use the existing textview design in this code?

Solution:

You just have a slight change in your code, Compare it from below code and try:

String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);

for( int i = 0; i < textArray.length; i++ )
{
    View v = (View) getLayoutInflater().inflate(R.layout.your_text_view_layout, null);
    TextView textView = v.findViewById(R.id.textviews);
    textView.setText(textArray[i]);
    linearLayout.addView(textView);
}

Hope it works.

You have to inflate TextView Layout, please try below code,

textviews.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textviews"
    android:layout_width="match_parent"
    android:layout_height="104dp"
    android:textSize="30sp" />

Java Code,

String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for( int i = 0; i < textArray.length; i++)
{
    TextView textView = (TextView) getLayoutInflater().inflate(R.layout.textviews, null);
    textView.setText(textArray[i]);
    linearLayout.addView(textView);
}

Take Linear Layout in xml,

 <LinearLayout
        android:id="@+id/linear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"/>

and add textview in it as

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linear);

        String[] textArray = {"One", "Two", "Three", "Four"};

        for( int i = 0; i < textArray.length; i++ )
        {
            TextView textView = new TextView(this);
            textView.setText(textArray[i]);
            linearLayout.addView(textView);
        }

As you want TextView below the TextView it has to be done by using ListView. First, you have to make a custom adapter layout.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#cede8a">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:padding="10dp"
    android:id="@+id/text"
    android:textColor="#000000"
    />
</LinearLayout>

And then make a list view in your layout where you want to show the TextView.

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

In main class you initialise the ListView and store the data in array or other data structure.

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private ArrayList<String> al_data = new ArrayList<>();
    private Adapter obj_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.listview);
        al_data.add("1234");
        al_data.add("1257");
        al_data.add("100");
        al_data.add("1547");

        obj_adapter = new Adapter(al_data);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false);
        listView.setLayoutManager(layoutManager);
        listView.setAdapter(obj_adapter);
    }
}

And now in the class of the custom adapter write this code.

public class Adapter extends ListView.Adapter<Adapter.MyViewHolder>{

    private ArrayList<String> al_data;

    public class MyViewHolder extends ListView.ViewHolder{
        public TextView textview;

        public MyViewHolder (View view){
            super(view);
            textview = view.findViewById(R.id.text);
        }
    }

    public Adapter(ArrayList<String> al_data) {
        this.al_data = al_data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        holder.textview.setText(al_data.get(position));
    }

    @Override
    public int getItemCount() {
        return al_data.size();
    }
}

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