espinchi espinchi - 5 months ago 27
Android Question

How to include a layout twice in Android?

Android's

<include />
element allows you to include other XML layouts. Useful for a common header across several activities.

But, what if you want to include a layout several times in the same view? For instance, I have a carefully crafted layout that I want to display three times in my view. Every of those instances would need different values. Since the
include
is basically a take that XML and paste it here, I'd need something more powerful.

Is there some mechanism to do this?

(Did I explain myself correctly?)

Answer

A blog post at 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 );