user3275095
user3275095

Reputation: 1635

Adding textview dynamically in linear layout

Sorry if this is duplicate post but I am not able to find my answer in similar posts.

So, my requirement is:

I have a linearlayout which have some textViews and editviews. Now I want to add one textview below existing editview at runtime.

The layout is a linear Layout and orientation is vertical.

I am very new to android programming. Any help will be appreciated.

Thanks

Upvotes: 1

Views: 607

Answers (3)

Arià
Arià

Reputation: 914

TextView textView = new TextView(mContext);
        textView.setText("Your text");
        mLinearLayout.addView(mTextView);

You can add any View in a LinearLayout using the method: addView

Upvotes: 0

Pontus Backlund
Pontus Backlund

Reputation: 1017

Either you have as many as you need in your xml with visibility:gone and switch them to mTextView.setVisibility(TextView.VISIBLE)or as the one above me who just answered

Upvotes: 1

Avijit
Avijit

Reputation: 3824

Use this:

TextView tv1 = new TextView(this);
tv1.setText("FIRST");
tv1.setTextSize(10);
tv1.setGravity(Gravity.CENTER);



LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setGravity(Gravity.CENTER);
ll.addView(tv1);  

Upvotes: 0

Related Questions