29

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?)

espinchi
  • 9,144
  • 6
  • 58
  • 65

4 Answers4

59

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 );
bk138
  • 3,033
  • 1
  • 34
  • 28
  • One small caveat is that it seems the Android lint still raises a warning about this: DuplicateIncludedIds: Duplicate ids across layouts combined with include tags – Yoel Gluschnaider Jan 28 '16 at 16:22
5

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • For the records, here's a good explanation (and link to docs) to learn how to declare custom attributes for your custom component: http://stackoverflow.com/questions/3441396/android-defining-custom-attrs – espinchi Dec 05 '10 at 12:17
3

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.

Javi
  • 19,387
  • 30
  • 102
  • 135
2

You can use android:id to specify the id of the root view of the included layout; it will also override the id of the included layout if one is defined. Similarly, you can override all the layout parameters.

Based on the provided android:id you can get the section by id, and then you can again get element by id based on the section you just retrieved. This way you will be able to lookup all child views with same ids, in each parent different id views in two steps.

Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • 5
    Can you get into trouble if the children have the same id? Only the views having an id have their sate automatically saved when the screen orientation change for instance. If two views have the same id would that cause problems with this? – Alex Jasmin Dec 04 '10 at 19:07
  • 3
    Yes. It cause problems as I suspected. If I put an EditText in a layout and include it twice from my main layout the text from second EditText is copied into the first when I rotate the screen (same as when two views have the same id without ``). If the EditText is at the *root* of the included layout it works as its id is replaced by the id from the `` tag. – Alex Jasmin Dec 04 '10 at 19:30
  • Cool. Thanks. Post this as a bug, and give me the link to vote on it. – Pentium10 Dec 04 '10 at 19:39
  • @Pentium10 I don't think I will. I'm not even sure it's actually a bug. I though android:id was supposed to be unique in the first place (though I may be wrong about that) which lead me to suspect this behavior. If *you* open a bug though post the link as I'm curious to see what responses it will get. – Alex Jasmin Dec 04 '10 at 19:47