0

I have a .xml file(picture below). In my .xml, the second row's views create while running.

I want my new created views get the same properties from the existing views, like textsize, maxlenght, text-alighn , etc

I tried to change the things that I set for first row views in xml, but some of my codes don't work. for example, this line:

            b2.setTextSize(b.getTextSize());

doesn't change the TextSize of the second Button.

what can I do?

enter image description here

Edit: Is there a function that could be able to get the whole attributes together and set them for new views?

N. N
  • 19
  • 6
  • Just inflate that same layout for each new row. – Mike M. Jan 27 '16 at 00:26
  • How can I inflate a layout for a specific button or TextView? – N. N Jan 28 '16 at 00:03
  • Create a separate layout file with whichever `View`s you want to add at runtime, inflate it like is shown [here](http://stackoverflow.com/questions/6070527/how-to-inflate-a-layout-dynamically), and then add it to your existing layout just like you're already doing for the second row. – Mike M. Jan 28 '16 at 00:14
  • Thank you. It fixed the attributes. but now I didn't set Id for any of them. so how can I access to their values? plus, this is just one row of my activity, do I have to repeat 10 xml files (each one for a special row)? – N. N Jan 28 '16 at 11:54
  • "I didn't set Id for none of them. so how can I access to their values?" - You need to set IDs for them, and then you can get references to the `View`s as demonstrated in the accepted answer in the linked post. "do I have to repeat one xml for any special row?" - I'm not quite sure what you're asking here, but you can inflate that layout as many times as you want. – Mike M. Jan 28 '16 at 11:59
  • Sorry, my english is bad I think. I used this line to set Id for a button from view. but my program gets error: b2 = (Button)view.findViewById(R.id.btn_add); ContainerLayout.addView(b2); – N. N Jan 28 '16 at 12:18
  • and for repeating...I said that because every row has different EditText and TextViews – N. N Jan 28 '16 at 12:20
  • "but my program gets error" - You're probably getting an `IllegalStateException` because `b2` is already a child of `view`. You want to add `view` to `ContainerLayout`, not `b2`. "every row has different EditText and TextViews" - Then you can create different layouts for each type of row you have, or you can create layout files that are just a singular `EditText` or `TextView`, inflate however many of each you need for a row, add them to a horizontal `LinearLayout`, and then add that `LinearLayout` to `ContainerLayout`. – Mike M. Jan 28 '16 at 12:27
  • Thank you so much. I changed "ContainerLayout.addView(b2);" to "ContainerLayout.addView(view);" Thanks again – N. N Jan 28 '16 at 12:34

0 Answers0