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

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


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.


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.