android – How to add TextView in Runtime? – Education Career Blog

How to add a new TextView to a layout in run time? is it possible?

,

Complete solution:

    View parent;  //parent view where to add


    ViewGroup layout=new LinearLayout(context);
    layout.setLayoutParams(new   ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    TextView tv1=new TextView(context);
    tv1.setText("Test1");
    TextView tv2=new TextView(context);
    tv2.setText("Test2");
    layout.addView(tv1);
    layout.addView(tv2);
    parent.addView(layout);

,

Programmatically adding TextView (or View in general) to layout (or ViewGroup in general) is possible, check ViewGroup’s public void addView (View child, int index) method.

Leave a Comment