Categories
Android

The merge tag

The merge tag allows you to avoid an unnecessary use of nested layouts when you use the include tag.
Consider the following example that consists of 3 files .xml as layout:

  • activity_main.xml

    this file .xml creates a vertical LinearLayout with a TextView inside and the contents of the file merge_1_level.xml using the include tag
  • merge_1_level.xml

    This file adds another TextView and the contents of the file merge_2_level.xm using the include tag
  • merge_2_level.xml

    this file adds the last TextView

The final result in the emulator is as follows:

The resulting layout is equivalent to:

As you can see the nested LinearLayouts are useless and redundant, to optimize the code you can use the merge tag in the included files: merge_1_level.xml and merge_2_level.xml

  • merge_1_level.xml
  • merge_2_level.xml

In this way the resulting layout is equivalent to:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.