Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 603
espinchi Avatar asked Dec 04 '10 18:12

espinchi


People also ask

How can use multiple layouts in one activity in Android?

You can use as many layouts as possible for a single activity but obviously not simultaneously. You can use something like: if (Case_A) setContentView(R. layout.

Is it possible to include one layout definition in another?

To efficiently re-use complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text.

Can you use a same XML layout for 2 different activities?

Yes you can! I had multiple activities inflate the same layout but they save different shared preferences.

How do I insert one XML file into another Android?

Probably you will have to set within the include tag the properties that sets where it has to be placed: android:layout_above, android:layout_toLeftOf, etc... Use fill_parent instead. I pasted my code and I am working with honeycomb, thats why I was using match_parent. But it should work with fill_parent.


4 Answers

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 );
like image 144
bk138 Avatar answered Oct 19 '22 16:10

bk138


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.

like image 5
CommonsWare Avatar answered Oct 19 '22 17:10

CommonsWare


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.

like image 3
Javi Avatar answered Oct 19 '22 17:10

Javi


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.

like image 2
Pentium10 Avatar answered Oct 19 '22 16:10

Pentium10