Linear Layout

A LinearLayout is a view group that puts all the children in a single direction either vertically
or horizontally. All children of a LinearLayout are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding). A LinearLayout respects margins between children and the gravity (right, center, or left alignment) of each child.

Most important attribute of the LinearLayout is orientation android:orientation="vertical"or android:orientation="horizontal"

When it is specified as vertical these are stacked one after the other in a vertical list and when its specified as horizontal these are arranged in a
horizontal list.
Some of the Important attributes of the LinearLayout.

1. Orientation

The arrangements of the views in LinearLayout as vertical or horizontal is controlled by the Orientation attribute.

2. layout_weight

The layout weight attribute specify each child control’s relative importance within the parent linear layout. layout_weight attribute is assigned to an individual view in the linearlayout. For example if an EditText is assigned 1 to its layout_weight attribute then it takes remaining space in its container as demonstrated in the below example



3. layout_gravity This is an other important property which defines the position of the view such right,left,top etc relative to other views in the leanerlayout,

Linear Layout Example

Please note the below XML Code for designing a simple Email Composer


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:paddingLeft="16dp"
   
android:paddingRight="16dp"
   
android:orientation="vertical" >
    <EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:hint="To" />
    <EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:hint="Subject" />
    <EditText
       
android:layout_width="match_parent"
       
android:layout_height="0dp"
       
android:layout_weight="1"
       
android:gravity="top"
       
android:hint="Message" />
    <Button
        
android:layout_width="100dp"
       
android:layout_height="wrap_content"
       
android:layout_gravity="right"
       
android:text="Send" />
</LinearLayout>

 

and output will be look like