0

I am trying to add a TextView from the activity when a button has been pressed. I have found how to add a new textview from the activity, however instead of coding the required layout parameters is it possible to copy an existing textviews parameters (in the xml layout) to the new textview?

I have tried:

TextView tv1 = new TextView(this);
TextView tv2 =  (TextView) findViewById(R.id.basetext);;

// its this line below which doesn't work
tv1.setLayoutParams(tv2.getLayoutParams());

But it doesn't copy any of the layout parameters...

Any ideas?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
James
  • 3
  • 3

2 Answers2

0

Did you call requestLayout() after adding the textview?

Prakash Nadar
  • 2,694
  • 1
  • 19
  • 20
0

You can do One thing.

If you are trying to set just the Value of the Same TextValue every time and if the Layout properties of that TextView is same at all the time then follow below steps:

  1. First Create the One Layout that only contain he TextView layout only with your appropriate properties. (Let name it as layout_textView.xml)

  2. Now, do add that layout_textView.xml dynamically to your main view as per your requirement.

How it will Solve your issue.

If any query then let me know.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • That worked thanks, also used [Making sense of LayoutInflater](http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater) to show how to insert the layout. – James Aug 06 '12 at 18:25