I have a common layout (common.xml) which I want to include many times in another layout (layout_a.xml). But it only shows me just one time. Why?
common.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:drawable/alert_light_frame">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/imageView"
android:src="@drawable/test"
android:scaleType="centerCrop" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="@+id/imageView"
android:id="@+id/textView"
android:text="test"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp" />
</RelativeLayout>
</merge>
layout_a.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/common" />
<include layout="@layout/common" />
</LinearLayout>
To efficiently reuse 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.
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.
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.
The ids when defined in XML must be unique. You are including two layouts that have views that contain the same id.
Here is how you would go about fixing it.
p.s. Unless there is more code that you are not including in your first layout file, that merge tag is useless.
As btse said, the ids in the XML must be unique. It can be achieved in this way:
<include android:id="@+id/common1"
layout="@layout/common" />
<include android:id="@+id/common2"
layout="@layout/common" />
For information about how to access the elements inside those two included views, you can check out this blog post.
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