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

57,500

Solution 1

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

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

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

<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

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

Share:
57,500

Related videos on Youtube

aioobe
Author by

aioobe

Previously: Senior Member of Technical Staff at Oracle, developing the java compiler. Cheering for this one right now: What is the difference between public, protected, package-private and private in Java? Most popular: Why is executing Java code in comments with certain Unicode characters allowed? How can I convert byte size into a human-readable format in Java? (apparently the most copied snippet on Stack Overflow!) Handling InterruptedException in Java For a recent bio, check out my web page, http://aioo.be. My StackRating badge: Also, check out my new web page: Programming.Guide

Updated on July 05, 2022

Comments

  • aioobe
    aioobe almost 2 years

    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.

  • aioobe
    aioobe about 14 years
    so the merge-thingy is referred to by its file-name... no id-attribute in the merge-file?
  • yanchenko
    yanchenko about 14 years
    @aioobe right. <include> basically means 'take that file and paste it's contents here'.
  • Adithya
    Adithya about 11 years
    hi, actually i am facing a grave problem here. I am using preferences and specifying layouts to use inside the preferences. Inside the layout i am using include-merger functionality (so that i have a place holder which will use switch or checkbox based on version). The problem is in my preferenceacvitity's onPostCreate method when i am trying to find the view (i.e. checkbox/switch), i am always getting the view as null ! Can you please help here ? stackoverflow.com/questions/15708599/…
  • Wayne Phipps
    Wayne Phipps almost 11 years
    Although the accepted answer is correct, I found that the article here: http://mfarhan133.wordpress.com/2010/10/01/reusing-layout-in‌​clude-merge-tag-for-‌​androd/ helped me visualise it better. Hope it helps someone.
  • Joshua Pinter
    Joshua Pinter about 10 years
    Is this doable with a XML menu declaration?
  • Yousha Aleayoub
    Yousha Aleayoub over 7 years
    What about IDs?
  • Carson J.
    Carson J. over 5 years
    Adding layout_width, layout_height, and tools:parentTag to the merge tag will have it show up in the preview for view_part. tools:parentTag="LinearLayout" in this example. Especially helpful for ConstraintLayouts.
  • Shamshirsaz.Navid
    Shamshirsaz.Navid over 3 years
    Thx for your useful explanation :)