Simple example of <merge> and <include> usage in Android XML-layouts

XmlAndroidLayoutMergeInclude

Xml Problem Overview


I'm curious about the <merge> and <include> tags in Android XML-layouts. I've read two tutorials, but haven't yet found a simple example usage.

Would be happy if someone could provide such an example or give a pointer to one.

Xml Solutions


Solution 1 - Xml

some_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
 
    // some views
    
    <include layout="@layout/view_part"/>

   // probably more views
    
</LinearLayout>

view_part.xml:

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    // the views to be merged
	
</merge>

Solution 2 - Xml

Take an example:

I have two tags <EditText> and <ListView > coming more than one UIs. So I created an XML file as given below to include in all such UI's.

<?xml ...>
<EditText ... />
<ListView ... />   

The above XML is not valid XML since it did not have a root element. So a root element is needed just for the sake of XML. <merge> is the solution as given below:

<?xml ...>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <EditText ... />
    <ListView ... />
</merge>

Solution 3 - Xml

There's a simple Android XML layout <include /> HOWTO that's also explaining a common pitfall over at http://www.coboltforge.com/2012/05/tech-stuff-layout/. That may help...

Solution 4 - Xml

<merge> tag is used to mitigate the number of the levels to increase the performance of rendering layouts. tag is used with <include> tag perfectly together.

Take an example, we have a login layout and used for more than one in scope of our app. While using tag to show login_layout, we can use and can escape a level.

I also advise you to read the tricks about layouts. http://android-developers.blogspot.com.tr/2009/03/android-layout-tricks-3-optimize-by.html

login_form.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Login form -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email..."
        android:inputType="textEmailAddress"
        android:maxLines="1"
        android:singleLine="true"
        android:visibility="visible" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password.."
        android:imeActionId="@+id/login"
        android:imeOptions="actionUnspecified"
        android:inputType="textPassword"
        android:maxLines="1"
        android:singleLine="true"
        android:text="1337"
        android:visibility="visible" />

    <Button
        android:id="@+id/sign_in_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="16sp"
        android:paddingLeft="32sp"
        android:paddingRight="32sp"
        android:text="Login"
        android:visibility="visible" />

</LinearLayout>

example_layout.xml (any layout we want to include login_form.xml)

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" >

    <include layout="@layout/login_form" />

</merge>

We can see the level hierarchy enter image description here

Solution 5 - Xml

id doesn't paste code otherwise relative layout parameters would have worked. It does some different processing

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaioobeView Question on Stackoverflow
Solution 1 - XmlyanchenkoView Answer on Stackoverflow
Solution 2 - XmlMohammed HView Answer on Stackoverflow
Solution 3 - Xmlbk138View Answer on Stackoverflow
Solution 4 - XmlhuseyinView Answer on Stackoverflow
Solution 5 - XmldevView Answer on Stackoverflow