espinchi espinchi - 1 year ago 77
Android Question

How to include a layout twice in Android?


<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
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 Source

A blog post at explains exactly that problem (the same layout XML included several times) and how to solve it!


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> -- id2
    -- stuff

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

View include_1 = findViewById(; 
View include_2 = findViewById(; 

and finally

include_2.findViewById( );