I often work with INCLUDE, but I never understood the MERGE. What are its uses and advantages?
I often work with INCLUDE, but I never understood the MERGE. What are its uses and advantages?
Include and Merge are separate things but are meant to be used together.
Include is used to reuse 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 your layout.
If you simply use one layout within the other (with include), you may end up having one parent inside another parent, for example a LinearLayout within another LinearLayout. This is too bad for the hierarchical structure of views 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. It 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 place 2 buttons directly into the destination layout by ignoring the merge tag.