Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 657
sandalone Avatar asked Feb 08 '12 15:02

sandalone


People also ask

Is it possible to include one layout definition in another?

To efficiently reuse complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text.

What is the correct way to attach a layout into your activity?

You can declare a layout in two ways: Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. You can also use Android Studio's Layout Editor to build your XML layout using a drag-and-drop interface.


2 Answers

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.

like image 190
onit Avatar answered Oct 11 '22 17:10

onit


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.

like image 21
Ajinkya Avatar answered Oct 11 '22 18:10

Ajinkya