3

This is a XML LinearLayout linlayout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mylinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>

I want to add TextViews to this layout programmatically because the number of TextViews to be added can be different sometimes.

Here is the activity code:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.linlayout);

    LinearLayout linear=(LinearLayout) findViewById(R.layout.mylinear);
    TextView [] txt =new TextView[3];

    for(int i=0;i<txt.length;i++)
    {
        txt[i]=new TextView(this);
        txt[i].setText("text "+i);
        txt[i].setLayoutParams(new
         LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        linear.addView(txt[i]);
    }
}

The LogCat don't display errors but the TextViews are not displayed when I run the app.

I try to put the line:

setContentView(R.layout.linlayout);

at the end, after the for, but doesn't work.

4
  • Did you check if the color of your text view's text isn't the same as the background color of your layout? Commented Oct 11, 2012 at 10:11
  • check by removing TextView txt =new TextView[3]; Commented Oct 11, 2012 at 10:15
  • TextView txt =new TextView[3]; change this line by TextView txt =new TextView[this]; Commented Oct 11, 2012 at 10:15
  • TextView [] txt =new TextView[3]; This is an array of 3 TextViews. Commented Oct 11, 2012 at 10:25

2 Answers 2

4

Use this :

TextView [] txt = new TextView[3];

for (int i=0; i<txt.length; i++) {
    txt[i] = new TextView(YourActivity.this);
    txt[i].setText("text " + i);
    txt[i].setLayoutParams(newLayoutParams
    (LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    linear.addView(txt[i]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

In my code this is Ok, just an error when I write the post, I edited it, sorry.
I copied your code and paste in my application it works fine.
Yes, it works, thanks, Using txt[i]=new TextView(YourActivity.this); works fine.
if this works fine. Then you have to accept this as right answer.
i am trying to add TextView progmmatically, but it does not work. public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RelativeLayout rl = (RelativeLayout) findViewById(R.layout.activity_main); final TextView tv = new TextView(MainActivity.this); tv.setText("Hello World"); tv.setTextColor(Color.BLACK); tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); rl.addView(tv); setContentView(R.layout.activity_main);}
1

It would be best to use a ListView. By the way did you change the orientation of your layout to a vertical orientation ? But if it necessary for you i suggesst this : i suppose you have an element with a certain size.

final int size = 3; // replace with the size of your element
LinearLayout linear = (LinearLayout) findViewById(R.layout.mylinear);

for(int i=0;i<size;i++){
    final TextView textView = new TextView(this);
    textView.setText("text "+i);
    textView.setLayoutParams(new LayoutParams(
    LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT));

    linear.addView(textView);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.