{"id":2034,"date":"2016-06-08T07:27:24","date_gmt":"2016-06-08T07:27:24","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?p=2034"},"modified":"2019-06-12T12:40:54","modified_gmt":"2019-06-12T12:40:54","slug":"nesting-of-layouts-android","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/ui\/nesting-of-layouts-android.html","title":{"rendered":"Nesting Of Layouts In Android With Example"},"content":{"rendered":"<p>Below is the example of Nesting Of Layouts\u00a0in which we create Nested Layout\u2019s. By the term of Nested we mean one Layout inside of other Layout. In Android all layout can be nested one another.<\/p>\n<p>In this example we create a Registration Form with multiple fields using Nested Linear Layouts. For that we set vertical orientation for parent Linear Layout and horizontal orientation for child Linear Layout.<\/p>\n<p>Below you can download code, see final output and step by step explanation:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/NestingOfLayouts\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2071\" src=\"\/ui\/wp-content\/uploads\/2016\/06\/Nesting-of-Layout-Example-in-Android.jpg\" alt=\"Nesting of Layout Example in Android\" width=\"238\" height=\"395\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Nesting-of-Layout-Example-in-Android.jpg 238w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Nesting-of-Layout-Example-in-Android-181x300.jpg 181w\" sizes=\"auto, (max-width: 238px) 100vw, 238px\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span>\u00a0Create a new project and name it NestingOfLayouts.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span>\u00a0Open 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 User Login Form using Nested Linear Layouts. For that we set vertical orientation for parent Linear Layout and horizontal orientation for child Linear Layout\u2019s. \u00a0In child Layouts we define TextView for label and EditText for entering value.<\/p>\n<pre>&lt;!-- Parent Layout --&gt;\r\n&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:background=\"@color\/whiteColor\"\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&lt;!-- first Child Layout --&gt;\r\n\r\n&lt;!-- create a Linear Layout with horizontal orientation and weightSum property --&gt;\r\n&lt;LinearLayout\r\nandroid:id=\"@+id\/firstLayout\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"50dp\"\r\nandroid:layout_marginTop=\"20dp\"\r\nandroid:orientation=\"horizontal\"\r\nandroid:weightSum=\"2\"&gt;\r\n\r\n&lt;!-- place one TextView and one EditText inside layout using weight property --&gt;\r\n\r\n&lt;TextView\r\nandroid:layout_width=\"0dp\"\r\nandroid:layout_height=\"50dp\"\r\nandroid:layout_marginRight=\"10dp\"\r\nandroid:layout_weight=\"0.6\"\r\nandroid:gravity=\"center_vertical\"\r\nandroid:paddingLeft=\"10dp\"\r\nandroid:text=\"User Name\"\r\nandroid:textColor=\"@color\/blackColor\" \/&gt;\r\n\r\n&lt;EditText\r\nandroid:id=\"@+id\/firstName\"\r\nandroid:layout_width=\"0dp\"\r\nandroid:layout_height=\"50dp\"\r\nandroid:layout_marginLeft=\"10dp\"\r\nandroid:layout_weight=\"1.4\"\r\nandroid:background=\"@color\/editTextBack\"\r\nandroid:hint=\"User Name\"\r\nandroid:imeOptions=\"actionNext\"\r\nandroid:paddingLeft=\"10dp\"\r\nandroid:singleLine=\"true\"\r\nandroid:textColor=\"@color\/blackColor\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n&lt;!-- create a Linear Layout with horizontal orientation and weightSum property --&gt;\r\n&lt;!-- second Child Layout --&gt;\r\n\r\n&lt;LinearLayout\r\nandroid:id=\"@+id\/secondLayout\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_marginTop=\"20dp\"\r\nandroid:orientation=\"horizontal\"\r\nandroid:weightSum=\"2\"&gt;\r\n\r\n&lt;!-- place one TextView and one EditText inside layout using weight property --&gt;\r\n\r\n&lt;TextView\r\nandroid:layout_width=\"0dp\"\r\nandroid:layout_height=\"50dp\"\r\nandroid:layout_marginRight=\"10dp\"\r\nandroid:layout_weight=\"0.6\"\r\nandroid:gravity=\"center_vertical\"\r\nandroid:paddingLeft=\"10dp\"\r\nandroid:text=\"Phone No.\"\r\nandroid:textColor=\"@color\/blackColor\" \/&gt;\r\n\r\n&lt;EditText\r\nandroid:id=\"@+id\/lastName\"\r\nandroid:layout_width=\"0dp\"\r\nandroid:layout_height=\"50dp\"\r\nandroid:layout_marginLeft=\"10dp\"\r\nandroid:layout_weight=\"1.4\"\r\nandroid:background=\"@color\/editTextBack\"\r\nandroid:hint=\"Phone No.\"\r\nandroid:imeOptions=\"actionNext\"\r\nandroid:paddingLeft=\"10dp\"\r\nandroid:singleLine=\"true\"\r\nandroid:textColor=\"@color\/blackColor\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n&lt;!-- create a Linear Layout with horizontal orientation and weightSum property --&gt;\r\n&lt;!-- third Child Layout --&gt;\r\n\r\n&lt;LinearLayout\r\nandroid:id=\"@+id\/thirdLayout\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_marginTop=\"20dp\"\r\nandroid:orientation=\"horizontal\"\r\nandroid:weightSum=\"2\"&gt;\r\n\r\n&lt;!-- place one TextView and one EditText inside layout using weight property --&gt;\r\n\r\n&lt;TextView\r\nandroid:layout_width=\"0dp\"\r\nandroid:layout_height=\"50dp\"\r\nandroid:layout_marginRight=\"10dp\"\r\nandroid:layout_weight=\"0.6\"\r\nandroid:gravity=\"center_vertical\"\r\nandroid:paddingLeft=\"10dp\"\r\nandroid:text=\"Email Id\"\r\nandroid:textColor=\"@color\/blackColor\" \/&gt;\r\n\r\n&lt;EditText\r\nandroid:id=\"@+id\/address\"\r\nandroid:layout_width=\"0dp\"\r\nandroid:layout_height=\"50dp\"\r\nandroid:layout_marginLeft=\"10dp\"\r\nandroid:layout_weight=\"1.4\"\r\nandroid:background=\"@color\/editTextBack\"\r\nandroid:hint=\"Email Id\"\r\nandroid:imeOptions=\"actionNext\"\r\nandroid:paddingLeft=\"10dp\"\r\nandroid:singleLine=\"true\"\r\nandroid:textColor=\"@color\/blackColor\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n&lt;!-- fourth Child Layout --&gt;\r\n\r\n&lt;LinearLayout\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_marginTop=\"20dp\"\r\nandroid:orientation=\"horizontal\"\r\nandroid:weightSum=\"2\"&gt;\r\n\r\n&lt;Button\r\nandroid:layout_width=\"0dp\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_marginRight=\"10dp\"\r\nandroid:layout_weight=\"1\"\r\nandroid:background=\"@color\/greenColor\"\r\nandroid:text=\"Cancel\"\r\nandroid:textColor=\"@color\/whiteColor\" \/&gt;\r\n\r\n&lt;Button\r\nandroid:layout_width=\"0dp\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_marginLeft=\"10dp\"\r\nandroid:layout_weight=\"1\"\r\nandroid:background=\"@color\/greenColor\"\r\nandroid:text=\"Submit\"\r\nandroid:textColor=\"@color\/whiteColor\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span>\u00a0Open src -&gt; package -&gt; MainActivity.java<\/p>\n<p>In this step we just show our default activity.<\/p>\n<pre>package example.abhiandroid.nestingoflayouts;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n@Override\r\nprotected void onCreate(Bundle savedInstanceState) {\r\nsuper.onCreate(savedInstanceState);\r\nsetContentView(R.layout.activity_main);\r\n}\r\n\r\n@Override\r\npublic boolean onCreateOptionsMenu(Menu menu) {\r\n\/\/ Inflate the menu; this adds items to the action bar if it is present.\r\ngetMenuInflater().inflate(R.menu.menu_main, menu);\r\nreturn true;\r\n}\r\n\r\n@Override\r\npublic boolean onOptionsItemSelected(MenuItem item) {\r\n\/\/ Handle action bar item clicks here. The action bar will\r\n\/\/ automatically handle clicks on the Home\/Up button, so long\r\n\/\/ as you specify a parent activity in AndroidManifest.xml.\r\nint id = item.getItemId();\r\n\r\n\/\/noinspection SimplifiableIfStatement\r\nif (id == R.id.action_settings) {\r\nreturn true;\r\n}\r\n\r\nreturn super.onOptionsItemSelected(item);\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span>\u00a0Open res -&gt; values-&gt; colors.xml<\/p>\n<p>In this step we define the colors (red, green and white) that used in the background and text color of view\u2019s.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;resources&gt;\r\n&lt;!-- define colors used in our xml file--&gt;\r\n&lt;color name=\"whiteColor\"&gt;#fff&lt;\/color&gt;\r\n&lt;color name=\"blackColor\"&gt;#000&lt;\/color&gt;\r\n&lt;color name=\"editTextBack\"&gt;#f2f2f2&lt;\/color&gt;\r\n&lt;color name=\"greenColor\"&gt;#0f0&lt;\/color&gt;\r\n&lt;\/resources&gt;\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App and you will see the below output<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2071\" src=\"\/ui\/wp-content\/uploads\/2016\/06\/Nesting-of-Layout-Example-in-Android-181x300.jpg\" alt=\"Nesting of Layout Output\" width=\"181\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Nesting-of-Layout-Example-in-Android-181x300.jpg 181w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Nesting-of-Layout-Example-in-Android.jpg 238w\" sizes=\"auto, (max-width: 181px) 100vw, 181px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is the example of Nesting Of Layouts\u00a0in which we create Nested Layout\u2019s. By the term of Nested we mean one Layout inside of other Layout. In Android all layout can be nested one another. In this example we create a Registration Form with multiple fields using Nested Linear Layouts. For that we set vertical &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/nesting-of-layouts-android.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Nesting Of Layouts In Android With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2071,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2034","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-archieve"],"acf":[],"psp_head":"<title>Nesting Of Layouts In Android With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Below is the example of Nesting Of Layouts in which we create Nested Layout\u2019s. By the term of Nested we mean one Layout inside of other Layout. In Android all layout can be nested one another.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/nesting-of-layouts-android.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/2034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/post"}],"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=2034"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/2034\/revisions"}],"predecessor-version":[{"id":2822,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/2034\/revisions\/2822"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media\/2071"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=2034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/categories?post=2034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/tags?post=2034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}