Can some clarify usage of <include> and <merge>

16,367

Solution 1

Yes you understood it correctly. merge is used as pseudo parent element to reduce the number of levels in view trees. Just check this link, it gives very good explanation of merge.

In your header file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <include
        android:id="@+id/header"
        layout="@layout/top"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>   

<LinearLayout> doesn't make any difference when your file is included in other file you mentioned. So it's a good thing to use merge instead.

Since in XML you must use a single parent element and the rest of the XML elements should be included in it, you should use merge as single parent element and can avoid adding unnecessary parent layout.

Just avoid 'merge' when you want to apply a layout differently than layout is defined in file in which your content is inclded.

Solution 2

From my understanding it will set the merge element as the higher element in the view hierarchy. Include will simply put the whole viewgroup in there. So using your example the view hierarchy should look like:

With merge:

LinearLayout (root)
|
TextView

With include:

LinearLayout (root)
|
LinearLayout
|
TextView

So you will have an extra LinearLayout in the view hierarchy that you do not need. However, sometimes you need that intermediate view. In your case, you wouldn't, since both the LinearLayouts have the same layout params and no other differences.

Share:
16,367

Related videos on Youtube

sandalone
Author by

sandalone

A developer since 1999, started with Java. A freelancer since 2009, and a client hiring freelancers since 2011. Have been using Android Studio and Gradle since 0.1.

Updated on June 06, 2022

Comments

  • sandalone
    sandalone almost 2 years

    I just need someone to tell me if I understood correctly when to use <include> and when <merge>.

    So, I make a header layout which I want to include into some other XML layout:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Header text" />
    </LinearLayout>
    

    And I include it into some other XML this way (which is pretty basic):

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <include
            android:id="@+id/header"
            layout="@layout/top"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    

    This will work well, no issue about it. But in order to optimize the code, I have to use <merge> in the layout which gets included. So the top layout should not have a tag <LinearLayout> but it must look like this:

    <merge xmlns:android="http://schemas.android.com/apk/res/android">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Header text" />
    </merge>
    

    Have I understood this correctly?

    • ahodder
      ahodder over 12 years
      Ya that looks good. Although, in my experience, I don't need merge. I cant just place the text view in its own xml file and include that file. No merge necessary.
    • sandalone
      sandalone over 12 years
      Yes, it will work, but is it really safe to use it that way because Google developers never suggested such practice. I am afraid what would happen if they decide to change something which recognizes merge tag.
    • auracool
      auracool over 9 years
      @sandalone like everything you will have to update the xml...to the new standard.There has been tons of depreciated methods in android since the beginning so this is not really a problem more of something that one should be mindful of.
    • Richard Le Mesurier
      Richard Le Mesurier over 7 years
      I found this post explains things really well: "Improve layout performance" on 101apps.co.za
  • sandalone
    sandalone over 12 years
    Thanks. The link you suggested made a confusion in my head :), and that's I wanted to double-check with you guys.
  • Ajinkya
    Ajinkya over 12 years
    @bergnam: Sorry for making confusion.I found it really helpful. Will share if I came across any other good link.
  • sandalone
    sandalone over 12 years
    It's ok. It's the official link after all, and all other texts are based on that one.