How to include a layout twice in Android? How to include a layout twice in Android? android android

How to include a layout twice in Android?


A blog post at http://www.coboltforge.com/2012/05/tech-stuff-layout/ (which is offline now but can be found at https://web.archive.org/web/20160425233147/http://www.coboltforge.com/2012/05/tech-stuff-layout/) explains exactly that problem (the same layout XML included several times) and how to solve it!

Edit

When you search by id you always find the first items, so the second widgets are hidden.

However, it can be solved

<include> -- id1    -- stuff</include><include> -- id2    -- stuff</include>

So we can find the subelements, by first looking up id2 / id1.

View include_1 = findViewById(R.id.id1); View include_2 = findViewById(R.id.id2); 

and finally

include_2.findViewById(R.id.elementx );


Is there some mechanism to do this?

Create a custom View. Here is a project where I have a ColorMixer custom widget, for example. You could include several such ColorMixers in one activity layout, if you so chose to. Each can have its own parameters to tailor its operation.


Another way to go could be setting the "template" layout in an xml and inflate it with LayoutInflater and add to your view as many times as you need and insert there the custom values in each one.Here is an example for Creating a Custom Toast View with Layout inflater.