What are the differences between <MERGE> and <INCLUDE>?

Asked

Viewed 208 times

3

I usually work with INCLUDE, but I never understood MERGE. What are its uses and advantages?

1 answer

4


Include and Merge are separate things but were meant to be used together.

Include is used to repurpose layouts, simple as that. But this creates a problem, because every layout defined in Android needs to start with a parent tag (a Viewgroup) that will contain all the elements of its layout.

If you simply use one layout inside the other (with include), you may end up having a parent inside another parent, for example a Linearlayout inside another Linearlayout. This is very bad for the hierarchical view structure on Android. That’s where the merge tag can help you.

The only way you can create a reusable layout without having to specify a parent tag (a Viewgroup such as Linearlayout) is by using the merge tag. Which will be disregarded when the system joins the two layouts.

In this example, the merge tag replaces the use of a Viewgroup (Linearlayout, Relativelayout, Framelayout, etc) to contain two buttons.

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

    <Button
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="@string/add"/>

    <Button
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="@string/delete"/>

</merge>

With this layout, when you use the include tag to use it, the system will put 2 buttons directly in the destination layout ignoring the merge tag.

  • Thank you for your reply. For those who do not understand, follow the link of the documentation, http://developer.android.com/training/improving-layouts/reusing-layouts.html.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.