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.