Skip to content

The merge Layout Tag

Question

When to use merge

Short interview answer

Use <merge> as a reusable layout’s root when its children should be inserted directly into an existing compatible parent, avoiding an otherwise redundant ViewGroup.

Detailed answer

A reusable pair of controls included inside an existing vertical parent does not need another vertical container merely to exist:

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

<merge> inserts these children directly into the including parent and removes a redundant wrapper. It is appropriate only when that parent supplies the necessary layout context.

Sources