Dynamically add textViews to a linearLayout Dynamically add textViews to a linearLayout android android

Dynamically add textViews to a linearLayout


Something like the following should be what you need:

final int N = 10; // total number of textviews to addfinal TextView[] myTextViews = new TextView[N]; // create an empty array;for (int i = 0; i < N; i++) {    // create a new textview    final TextView rowTextView = new TextView(this);    // set some properties of rowTextView or something    rowTextView.setText("This is row #" + i);    // add the textview to the linearlayout    myLinearLayout.addView(rowTextView);    // save a reference to the textview for later    myTextViews[i] = rowTextView;}


You can add TextViews at runtime by following code below:

LinearLayout lLayout = (LinearLayout) findViewById(R.id.linearlayout2); // Root ViewGroup in which you want to add textviewsfor (int i = 0; i < 5; i++) {    TextView tv = new TextView(this); // Prepare textview object programmatically    tv.setText("Dynamic TextView" + i);    tv.setId(i + 5);    lLayout.addView(tv); // Add to your ViewGroup using this method}


I think this will be useful:

int j = 0;context.getSystemService(Context.WINDOW_SERVICE);WindowManager manager = (WindowManager) context                        .getSystemService(Context.WINDOW_SERVICE);Display display = manager.getDefaultDisplay();for (int i = 0; i < tabsize; i++) {    Tab tab = tabSet.get(i);    if (i == selectedTabId)        tab.setSelected(true);    View view = tab.getView();    TableRow.LayoutParams pCol = new TableRow.LayoutParams();    pCol.width = display.getWidth() / tabSet.size();    rowBottom.addView(view, pCol);}