mohammedsuhail
mohammedsuhail

Reputation: 701

How to add TextView in Runtime?

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

Upvotes: 2

Views: 7119

Answers (2)

Barmaley
Barmaley

Reputation: 16363

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);

Upvotes: 5

Ralkie
Ralkie

Reputation: 3736

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.

Upvotes: 1

Related Questions