{"id":338,"date":"2015-12-26T11:47:16","date_gmt":"2015-12-26T11:47:16","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=338"},"modified":"2018-06-05T10:00:52","modified_gmt":"2018-06-05T10:00:52","slug":"linear-layout","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/linear-layout","title":{"rendered":"Linear Layout Tutorial With Examples In Android"},"content":{"rendered":"<p>Linear layout is a simple layout used in android for layout designing. In the Linear layout all the elements are displayed in linear fashion means all the childs\/elements of a linear layout are displayed according to its orientation. The value for orientation property can be either horizontal\u00a0or\u00a0vertical.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-349\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Horizontal-And-Vertical-Orientation-In-Linear-Layout.jpg\" alt=\"Horizontal And Vertical Orientation In Linear Layout\" width=\"452\" height=\"142\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Horizontal-And-Vertical-Orientation-In-Linear-Layout.jpg 452w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Horizontal-And-Vertical-Orientation-In-Linear-Layout-300x94.jpg 300w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Types Of Linear Layout Orientation<\/strong><\/h4>\n<p>There are two types of linear layout orientation:<\/p>\n<ol>\n<li>Vertical<\/li>\n<li>Horizontal<\/li>\n<\/ol>\n<p>As the name specified these two orientations are used to arrange there child one after the other, in a line, either vertically or horizontally. Let\u2019s we describe these in detail.<\/p>\n<p><strong><span style=\"color: #008000;\">1.Vertical:<\/span> <\/strong><\/p>\n<p>In this all the child are arranged vertically in a line one after the other. In below code snippets we have specified orientation \u201cvertical\u201d so the childs\/views of this layout are displayed vertically.<\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:orientation=\"vertical\"&gt; &lt;!-- Vertical Orientation set --&gt;\r\n\r\n    &lt;!-- Child Views(In this case 2 Button) are here --&gt;\r\n\r\n\r\n    &lt;Button\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button1\"\r\n        android:id=\"@+id\/button\"\r\n        android:background=\"#358a32\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button2\"\r\n        android:id=\"@+id\/button2\"\r\n        android:background=\"#0058b6\" \/&gt;\r\n\r\n \r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-344\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Vertical-Orientation-in-Linear-Layout.jpg\" alt=\"Vertical Orientation in Linear Layout\" width=\"249\" height=\"132\" \/><\/center><span style=\"color: #008000;\"><strong>2. Horizontal:<\/strong><\/span><\/p>\n<p>In this all the child are arranged horizontally in a line one after the other. In below code snippets we have specified orientation \u201chorizontal\u201d so the childs\/views of this layout are displayed horizontally.<\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:orientation=\"horizontal\"&gt; &lt;!-- Horizontal Orientation set --&gt;\r\n\r\n    &lt;!-- Child Views(In this case 2 Button) are here --&gt;\r\n\r\n\r\n    &lt;Button\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button1\"\r\n        android:id=\"@+id\/button\"\r\n        android:background=\"#358a32\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button2\"\r\n        android:id=\"@+id\/button2\"\r\n        android:background=\"#0058b6\" \/&gt;\r\n\r\n\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-345\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Horizontal-orientation-in-Linear-Layout.jpg\" alt=\"Horizontal orientation in Linear Layout\" width=\"246\" height=\"118\" \/><\/center><span style=\"text-decoration: underline; color: #ff0000;\"><strong>Important Note:<\/strong><\/span> All of the layout managers can be nested. This means that you can put a Relative Layout or Frame Layout as a child to Linear Layout.<\/p>\n<hr \/>\n<h4><strong>Main Attributes In Linear Layout:<\/strong><\/h4>\n<p>Now let\u2019s \u00a0we discuss about the attributes that helps us to configure a linear layout and its child controls. Some of the most important attributes you will use with linear layout include:<\/p>\n<p><span style=\"color: #008000;\"><strong>1. orientation:<\/strong><\/span> The orientation attribute used to set the childs\/views horizontally or vertically. In Linear layout default orientation is vertical.<\/p>\n<p><strong>Example: \u00a0Orientation vertical:<\/strong><\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:orientation=\"vertical\"&gt; &lt;!-- Vertical Orientation set --&gt;\r\n\r\n    &lt;!-- Put Child Views like Button here --&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><strong>Example: Orientation Horizontal:<\/strong><\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:orientation=\"horizontal\"&gt; &lt;!-- Horizontal Orientation set --&gt;\r\n\r\n    &lt;!-- Child Views are here --&gt;\r\n\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-349\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Horizontal-And-Vertical-Orientation-In-Linear-Layout.jpg\" alt=\"Horizontal And Vertical Orientation In Linear Layout\" width=\"452\" height=\"142\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Horizontal-And-Vertical-Orientation-In-Linear-Layout.jpg 452w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Horizontal-And-Vertical-Orientation-In-Linear-Layout-300x94.jpg 300w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/center><span style=\"color: #008000;\"><strong>2. gravity:<\/strong><\/span> The gravity attribute is an optional attribute which is used to control the alignment of the layout like left, right, center, top, bottom etc.<\/p>\n<p><strong>Example: We have set gravity right for linear layout. So the buttons gets align from right side in Horizontal orientation.<br \/>\n<\/strong><\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:gravity=\"right\"\r\n    android:orientation=\"horizontal\"&gt;\r\n\r\n\r\n&lt;!--Button Child View Here---&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button2\"\r\n        android:id=\"@+id\/button2\"\r\n        android:background=\"#0e7d0d\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button1\"\r\n        android:id=\"@+id\/button\"\r\n        android:background=\"#761212\" \/&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-350\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Gravity-in-Android-Linear-Layout.jpg\" alt=\"Gravity in Android Linear Layout\" width=\"328\" height=\"156\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Gravity-in-Android-Linear-Layout.jpg 328w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Gravity-in-Android-Linear-Layout-300x143.jpg 300w\" sizes=\"auto, (max-width: 328px) 100vw, 328px\" \/><\/center><span style=\"color: #008000;\"><strong>3. layout_weight:<\/strong><\/span> The layout weight attribute specify each child control\u2019s relative importance within the parent linear layout.<\/p>\n<p><strong>Example: weight property for button in linear layout. In the below example one button is of weight 2 and other is of weight 1.<\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"horizontal\"&gt;\r\n\r\n\r\n&lt;!--Add Child View Here---&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Weight 2\"\r\n        android:background=\"#761212\"\r\n        android:layout_margin=\"5dp\"\r\n        android:id=\"@+id\/button\"\r\n        android:layout_weight=\"2\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:background=\"#761212\"\r\n        android:layout_margin=\"5dp\"\r\n        android:layout_weight=\"1\"\r\n        android:text=\"Weight 1\" \/&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><strong>In the layout image you can notice Button with weight 2 gets more size related the other.<\/strong><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-351\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Weight-in-Linear-Layout.jpg\" alt=\"Weight in Linear Layout\" width=\"337\" height=\"160\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Weight-in-Linear-Layout.jpg 337w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Weight-in-Linear-Layout-300x142.jpg 300w\" sizes=\"auto, (max-width: 337px) 100vw, 337px\" \/><\/center><span style=\"color: #008000;\"><strong>4. weightSum:<\/strong><\/span> weightSum is the sum up of all the child attributes weight. This attribute is required if we define weight property of the childs.<\/p>\n<p><strong>Example: In the same above example of weight, we can define weightSum value 3.<\/strong><\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:weightSum=\"3\"\r\n    android:orientation=\"horizontal\"&gt;\r\n    &lt;!--Add Child View Here---&gt;\r\n\r\n&lt;\/LinearLayout&gt;<\/pre>\n<hr \/>\n<h4><strong>Example of Linear Layout:<\/strong><\/h4>\n<p>Now lets design 2 linear layout UI. First we have designed using weight attribute and second without using it. So below layout output will clear the difference between them:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-360\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Linear-Layout-in-Android-Output.jpg\" alt=\"Linear Layout in Android Output\" width=\"626\" height=\"358\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Linear-Layout-in-Android-Output.jpg 626w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Linear-Layout-in-Android-Output-300x172.jpg 300w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><\/center><span style=\"color: #008000;\"><strong>Example 1:<\/strong><\/span> First we will design Android Linear Layout without using weight property<\/p>\n<p>In this example we have used one TextView and 4 Button. The orientation is set to vertical.<\/p>\n<p><strong>Below is the code of activity_main.xml<\/strong><\/p>\n<pre>&lt;!-- Vertical Orientation is set --&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"vertical\"&gt;\r\n\r\n    &lt;!-- Text Displayed At Top --&gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n        android:text=\"Linear Layout (Without Weight)\"\r\n        android:id=\"@+id\/textView\"\r\n        android:layout_gravity=\"center_horizontal\" \/&gt;\r\n\r\n    &lt;!-- Button Used --&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button 1\"\r\n        android:background=\"#009300\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button 2\"\r\n        android:background=\"#e6cf00\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button 3\"\r\n        android:background=\"#0472f9\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button 4\"\r\n        android:background=\"#e100d5\" \/&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Output Screen:<\/strong><\/span><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-356\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Linear-Layout-in-Android-Example-1.jpg\" alt=\"Linear Layout in Android Example 1\" width=\"319\" height=\"339\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Linear-Layout-in-Android-Example-1.jpg 319w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Linear-Layout-in-Android-Example-1-282x300.jpg 282w\" sizes=\"auto, (max-width: 319px) 100vw, 319px\" \/><\/center><span style=\"color: #008000;\"><strong>Example 2:<\/strong><\/span> In this example of linear layout we have used weight property.<\/p>\n<p><strong>Below is the code of activity_main.xml with explanation included<\/strong><\/p>\n<pre>&lt;!-- Vertical Orientation is set  with weightSum--&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:weightSum=\"5\"\r\n    android:orientation=\"vertical\"&gt;\r\n\r\n    &lt;!-- Text Displayed At Top --&gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n        android:text=\"Linear Layout (With Weight)\"\r\n        android:id=\"@+id\/textView\"\r\n        android:layout_gravity=\"center_horizontal\"\r\n        android:layout_weight=\"0\"\/&gt;\r\n\r\n    &lt;!-- Button Used --&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button 1\"\r\n        android:background=\"#009300\"\r\n        android:layout_weight=\"1\"\/&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button 2\"\r\n        android:background=\"#e6cf00\"\r\n        android:layout_weight=\"1\"\/&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button 3\"\r\n        android:background=\"#0472f9\"\r\n        android:layout_weight=\"1\"\/&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button 4\"\r\n        android:background=\"#e100d5\"\r\n        android:layout_weight=\"1\"\/&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><strong>Output Screen:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-357\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Linear-Layout-with-weight-in-Android-Example-2.jpg\" alt=\"Linear Layout with weight in Android Example 2\" width=\"314\" height=\"446\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Linear-Layout-with-weight-in-Android-Example-2.jpg 314w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Linear-Layout-with-weight-in-Android-Example-2-211x300.jpg 211w\" sizes=\"auto, (max-width: 314px) 100vw, 314px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linear layout is a simple layout used in android for layout designing. In the Linear layout all the elements are displayed in linear fashion means all the childs\/elements of a linear layout are displayed according to its orientation. The value for orientation property can be either horizontal\u00a0or\u00a0vertical. Types Of Linear Layout Orientation There are two &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/linear-layout\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Linear Layout Tutorial With Examples In Android<\/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-338","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>Linear Layout Tutorial With Examples In Android \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"The tutorial on linear layout discussing both orientation vertical, horizontal and attributes with examples, code and screenshot. Learn to design linear Android UI.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/linear-layout\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/338","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=338"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/338\/revisions"}],"predecessor-version":[{"id":2637,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/338\/revisions\/2637"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}