Warning: This <FrameLayout> can be replaced with a <merge> tag

Oleksiy picture Oleksiy · Jan 31, 2014 · Viewed 8.1k times · Source

I have a FrameLayout that contains a TextView and two LinearLayouts:

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


    ... a textview and 2 linearlayouts


</FrameLayout>

After running Android Lint, I get this warning: This <FrameLayout> can be replaced with a <merge> tag.

Why does this warning exist? What can I do to fix it (other than ignore)?

Answer

Aswin Rajendiran picture Aswin Rajendiran · Jan 31, 2014

To understand this, you need to understand how layouts are inflated and placed.

Let's say for example, you have an activity and this is the layout xml you use. Here is what the layout of the activity looks before you put your layout file.

<FrameLayout> // This is the window
    <FrameLayout> // This is activity
    </FrameLayout>
</FrameLayout>

There might be few other layers depending on the device/OS.

Now when you inflate your layout file and put it in, this is how it will look.

<FrameLayout> // This is the window
    <FrameLayout> // This is activity
            //A textview and 2 linearlayouts
    </FrameLayout>
</FrameLayout>

Do you see the FrameLayout inside the other FrameLayout? It is redundant to have that since it does not add much value. To optimize, you can replace your outer FrameLayout with the <merge> tag. This is what it will look like.

<merge> // This is the window
    <FrameLayout> // This is activity
            //A textview and 2 linearlayouts
    </FrameLayout>
</merge>

Notice how there is no extra FrameLayout. Instead, it is just merged with the FrameLayout of the activity. Whenever you can, you should use <merge>. This doesn't only apply to FrameLayouts. You can read more about it here. http://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge

Hope this helps.