77

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
  • 413,195
  • 112
  • 811
  • 826
  • 2
    Please take a look at the official Android documentation: [Re-using Layouts with ](http://developer.android.com/training/improving-layouts/reusing-layouts.html) – JJD Feb 13 '13 at 16:08
  • http://stackoverflow.com/a/11093340/596555 ,may be help u. – boiledwater Aug 06 '13 at 03:28
  • FYI, if you're looking to use this with **menus**, you're out of luck, but you can inflate multiple XML files, as described here: http://stackoverflow.com/questions/4337034/include-menu-in-menu-android – Joshua Pinter May 09 '14 at 02:09

5 Answers5

98

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>
yanchenko
  • 56,576
  • 33
  • 147
  • 165
  • so the merge-thingy is referred to by its file-name... no id-attribute in the merge-file? – aioobe Apr 28 '10 at 20:09
  • 19
    @aioobe right. `` basically means 'take that file and paste it's contents here'. – yanchenko Apr 28 '10 at 20:28
  • 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 ? http://stackoverflow.com/questions/15708599/findviewbyid-returns-null-for-preference-layout – Adithya Mar 30 '13 at 15:53
  • 3
    Although the accepted answer is correct, I found that the article here: [http://mfarhan133.wordpress.com/2010/10/01/reusing-layout-include-merge-tag-for-androd/](http://mfarhan133.wordpress.com/2010/10/01/reusing-layout-include-merge-tag-for-androd/) helped me visualise it better. Hope it helps someone. – Wayne Phipps Jun 08 '13 at 16:30
  • Is this doable with a XML menu declaration? – Joshua Pinter May 08 '14 at 01:41
  • 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. – Carson J. Feb 06 '19 at 18:35
8

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>
Mohammed H
  • 6,880
  • 16
  • 81
  • 127
5

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...

bk138
  • 3,033
  • 1
  • 34
  • 28
3

<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

huseyin
  • 1,367
  • 16
  • 19
2

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

dev
  • 21
  • 1