I have created a Layout file for an activity. In this layout I have created a LinearLayout with a textview and an edittext. Now I want to create additional LinearLayouts that will look and contain the exact same views that my original LinearLayout, but with different texts. I also want to do it programmatically during run because the amount of these LinearLayout will vary between runs. I've read some about inflaters but I don't understand them enough to use them.
I'm thinking something like this, obviously the code is wrong, but hopefully you understand what I want to do:
LinearLayout llMain = (LinearLayout)findViewById(R.id.mainLayout);
LinearLayout llToCopy = (LinearLayout)findViewById(R.id.linearLayoutToCopy);
for(int player = 0; player < size; player++)
{
LinearLayout llCopy = llToCopy.clone();
TextView tv = (TextView)llCopy.getChildAt(0);
tv.setText(players.get(player).getName());
llMain.addView(llCopy);
}
You can achieve that by using layout inflater
get the layout inflatter by using this
There are several ways to accomplish this.
A quick and easy approach is to inflate a new layout in every iteration of the loop:
Another (more elegant) solution is to create a separate class extending LinearLayout:
Now you can create a
ChildView
in every iteration of your loop and set the text via thesetText(String text)
method: