{"id":1864,"date":"2016-05-23T11:26:21","date_gmt":"2016-05-23T11:26:21","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=1864"},"modified":"2019-06-12T09:50:51","modified_gmt":"2019-06-12T09:50:51","slug":"include-merge-tag","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/include-merge-tag","title":{"rendered":"Include Merge Tag Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, for reusing the complete layouts we can use the <code>&lt;include\/&gt;<\/code> and <code>&lt;merge\/&gt;<\/code> tags to embed another layout inside the current layout. Android offers a variety of widgets to provide small and reusable interactive elements. In some cases\u00a0we might also need to reuse larger components that require a special layout.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1925\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/Include-And-Merge-Tag-In-Android.jpg\" alt=\"Include And Merge Tag In Android\" width=\"932\" height=\"247\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/Include-And-Merge-Tag-In-Android.jpg 932w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/Include-And-Merge-Tag-In-Android-300x80.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/Include-And-Merge-Tag-In-Android-768x204.jpg 768w\" sizes=\"auto, (max-width: 932px) 100vw, 932px\" \/><\/p>\n<p>Reusing layouts is powerful as it allows us to create reusable complex layouts. It also means that any elements of our application that are common across multiple layouts can be extracted, managed separately and then included in each layout. For example, in many\u00a0cases we need custom title bar with description text and icon, then we can create a custom title bar layout and use this tag (i.e.\u00a0&lt;include\/&gt; and &lt;merge\/&gt;) to re-use the same layout in multiple activities.<\/p>\n<hr \/>\n<h4><strong>Include Tag:<\/strong><\/h4>\n<p>The &lt;include\/&gt; tag helps us to\u00a0include the xml content\u2019s in our xml file. This is\u00a0a nice way to share layout parts between different layout\u2019s. Suppose we need to show\u00a0the custom title in many\u00a0layouts,\u00a0for that\u00a0we simply create a custom layout for title bar and then reuse the layout by using &lt;include\/&gt; tag.<\/p>\n<p><strong>Basic\u00a0include Tag\u00a0XML code in Android:<\/strong><\/p>\n<pre>&lt;include android:id=\"@+id\/custom_title_layout\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nlayout=\"@layout\/custom_title\"\/&gt;\r\n<\/pre>\n<hr \/>\n<h4><strong>Merge Tag:<\/strong><\/h4>\n<p>The\u00a0&lt;merge \/&gt;\u00a0tag helps us to eliminate redundant view groups in our view hierarchy when including one layout within another. For example, if our main layout is LinearLayout with vertical orientation\u00a0in which two consecutive views can be re-used in multiple layouts, then the re-usable layout in which you place the two views requires its own root view. However, using another\u00a0LinearLayout\u00a0as the root of the re-usable layout would result in a vertical LinearLayout\u00a0inside a vertical\u00a0LinearLayout. The nested\u00a0LinearLayout\u00a0serves no real purpose other than to slow down our UI performance. To avoid including such a redundant view group, we can instead use the\u00a0&lt;merge&gt;\u00a0element as the root view for the re-usable layout.<\/p>\n<p><strong>Basic\u00a0merge Tag XML code:<\/strong><\/p>\n<pre>&lt;merge xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"&gt;\r\n\r\n\u00a0\r\n&lt;Button\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:text=\"First\"\/&gt;\r\n\r\n&lt;Button\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:text=\"Second\"\/&gt;\r\n\r\n&lt;\/merge&gt;\r\n<\/pre>\n<p>Now, when we include this layout in another layout (using the\u00a0&lt;include\/&gt;\u00a0tag), the system ignores the &lt;merge&gt;\u00a0element and places the two Buttons directly in the layout, in place of the\u00a0&lt;include\/&gt;\u00a0tag.<\/p>\n<hr \/>\n<h4><strong>Difference between ViewStub and include tag:<\/strong><\/h4>\n<p>The include tag will just include the xml content\u2019s in our xml file. It\u2019s a nice way to share layout parts between different layout\u2019s. The <a href=\"\/ui\/viewstub\">ViewStub<\/a> tag is a little bit different because it is not directly included, and will be loaded only when you actually need it, i.e when you set ViewStub\u2019s visibility to \u201ctrue\u201d. This is a nice optimization because you could have a complex layout with tons of small views, complex views or headers anywhere, and still have your Activity load up really fast. Once you use one of those views, it\u2019ll be loaded.<\/p>\n<hr \/>\n<h4><strong>Attributes of include:<\/strong><\/h4>\n<p>Now let\u2019s we discuss some common attributes of a include tag that helps us to configure it in our layout (xml).<\/p>\n<p><span style=\"color: #008000;\"><strong>1. id:<\/strong><\/span> This attribute is used to uniquely identify a include tag.<\/p>\n<p>Below we set the \u201ccustom_title_bar\u201d id of the include tag that is used to uniquely identify it.<\/p>\n<pre>&lt;include android:id=\"@+id\/custom_title_bar\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\/&gt; &lt;!-- id of the include tag that is used to uniquely identify it--&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. layout:<\/strong><\/span> This attribute is used to supply an identifier for the layout resource to include a custom layout in our main layout.<\/p>\n<p>Below we include a custom layout i.e custom _title in our main layout using include tag.<\/p>\n<pre>&lt;include android:id=\"@+id\/custom_title_bar\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nlayout=\"@layout\/custom_title\"\/&gt; &lt;!-- include custom_title layout in other layout --&gt;\r\n<\/pre>\n<hr \/>\n<h4>include and merge Tag Example In Android Studio:<\/h4>\n<p>Below is the example of include and merge tag in which firstly we create a custom layout with a ImageView and a Button by using merge tag and then include that layout in our main layout using include tag. Finally we access the custom layout\u2019s view\u2019s in our Activity and perform click event on Button. Whenever a user click on Button a toast with message \u201c Custom Layout\u2019s Button Clicked \u201d \u00a0displayed.<\/p>\n<p>Below you can download code, see final output and step by step explanation of the example:<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/github.com\/abhisheksaini4\/IncludeTagExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ?<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1928\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/include-and-merge-Tag-Example-In-Android-Studio.jpg\" alt=\"include and merge Tag Example In Android Studio\" width=\"226\" height=\"346\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/include-and-merge-Tag-Example-In-Android-Studio.jpg 226w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/include-and-merge-Tag-Example-In-Android-Studio-196x300.jpg 196w\" sizes=\"auto, (max-width: 226px) 100vw, 226px\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span>\u00a0Create a new project and name it IncludeTagExample<\/p>\n<p><strong><span style=\"color: #008000;\">Step 2:<\/span>\u00a0<\/strong>Open res -&gt; layout -&gt;activity_main.xml (or) main.xml and add following code:<\/p>\n<p>In this step we open xml file and then create a TextView and also include a custom_layout file using include tag.<\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nxmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:orientation=\"vertical\"\r\nandroid:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\nandroid:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\nandroid:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\nandroid:paddingTop=\"@dimen\/activity_vertical_margin\"\r\ntools:context=\".MainActivity\"&gt;\r\n\r\n&lt;TextView\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_gravity=\"center\"\r\nandroid:text=\"Example Of Include And Merge Tag\"\r\nandroid:textColor=\"#00f\"\r\nandroid:layout_marginBottom=\"100dp\"\r\nandroid:textSize=\"20sp\" \/&gt;\r\n\r\n&lt;include\r\nandroid:id=\"@+id\/custom_layout\"\r\nlayout=\"@layout\/custom_layout\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span>\u00a0Create a new xml file i.e custom_layout.xml inside layout and add the below code:<\/p>\n<p>In this file we create a Button and a ImageView using merge tag to display in other activities.<\/p>\n<pre>&lt;merge xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"&gt;\r\n\r\n&lt;Button\r\nandroid:id=\"@+id\/customButton\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:background=\"#00f\"\r\nandroid:text=\"Included Layout's Button\"\r\nandroid:textColor=\"#fff\"\r\nandroid:textSize=\"18sp\"\r\nandroid:textStyle=\"bold\" \/&gt;\r\n\r\n&lt;ImageView\r\nandroid:id=\"@+id\/customImageView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:src=\"@drawable\/ic_launcher\" \/&gt;\r\n\r\n&lt;\/merge&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:\u00a0<\/strong><\/span>Open\u00a0 \u00a0src -&gt; package -&gt; MainActivity.java<\/p>\n<p>In this step Firstly we get the reference of included layout\u2019s Button and ImageView and finally perform click event on Button. Whenever a user click on Button a toast with message \u201c Custom Layout\u2019s Button Clicked \u201d \u00a0displayed.<\/p>\n<pre>package example.abhiandroid.includetagexample;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.ImageView;\r\nimport android.widget.Button;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\nButton customButton;\r\nImageView customImageView;\r\n\r\n@Override\r\nprotected void onCreate(Bundle savedInstanceState) {\r\nsuper.onCreate(savedInstanceState);\r\nsetContentView(R.layout.activity_main);\r\ncustomButton = (Button) findViewById(R.id.customButton); \/\/ get the reference of custom Layout's Button\r\ncustomImageView = (ImageView) findViewById(R.id.customImageView); \/\/ get the reference of custom Layout's ImageView\r\ncustomButton.setOnClickListener(new View.OnClickListener() {\r\n@Override\r\npublic void onClick(View v) {\r\n\/\/ display a toast on Button Click\r\nToast.makeText(getApplicationContext(), \"Custom Layout\u2019s Button Clicked\", Toast.LENGTH_LONG).show();\r\n}\r\n});\r\n}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Android, for reusing the complete layouts we can use the &lt;include\/&gt; and &lt;merge\/&gt; tags to embed another layout inside the current layout. Android offers a variety of widgets to provide small and reusable interactive elements. In some cases\u00a0we might also need to reuse larger components that require a special layout. Reusing layouts is powerful &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/include-merge-tag\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Include Merge Tag Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-1864","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>Include Merge Tag Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"In Android, for reusing the complete layouts we can use the  and  tags to embed another layout inside the current layout. Android offers a variety of widgets to provide small and reusable interactive elements.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/include-merge-tag\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=1864"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1864\/revisions"}],"predecessor-version":[{"id":2791,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1864\/revisions\/2791"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=1864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}