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?)
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.
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.
Yes you can! I had multiple activities inflate the same layout but they save different shared preferences.
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.
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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With