Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we create dynamic textview?

Tags:

android

how to create the textview in code not in xml file. It is because number of textviews will be changing in my application according to some integer.

like image 951
Shane Avatar asked Dec 12 '25 15:12

Shane


2 Answers

This is the code to create TextView Dynamically

LinearLayout layout = (LinearLayout ) findViewById(R.id.llayout);

for (int i = 0; i < 3; i++) {

TextView dynamicTextView = new TextView(this);
dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        dynamicTextView.setText("NewYork");
        layout.addView(tstate);

}
like image 93
Nishant Avatar answered Dec 15 '25 03:12

Nishant


Maybe thats what you need:

LinearLayout lin = (LinearLayout) findViewById(R.id.myLinear);

  for (int i = 0; i <= 10 ; i++)
  {
      TextView myText = new TextView(this);
      myText.setText("textview# "+ i);
      lin.addView(myText);
  }
like image 24
Thkru Avatar answered Dec 15 '25 05:12

Thkru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!