Introduction of Android Layout Tutorial :

LinearLayout : A Layout that arranges its children in a single column or a single row either Horizontally or Vertically using orientation method. However the Horizontal is the default behaviour of the LinearLayout.  In additional, the highest “weight” component will fill up the remaining space in LinearLayout.

Each layout has a set of attributes which define the visual properties of that layout.Among them some Common Attributes used in LinearLayout via .xml file.

Attributes Name Use
android:id ID which uniquely identifies the viewIt would be any name.

Ex. android:id=”+@id/id_name”

android:layout_width Width if the layoutYou can given width as like below,

–          fill_parent : Covers all the areas of parent view width wise.

–          wrap_content : Covers width based on your content’s size.

–          match_parent : Contains as much width as its parent has.

Ex. android:layout_width=”fill_parent”

Or you can specify int value as well, which is counted as dip value. (Dimensions In Pixels)

Ex. android:layout_width=”200dip”

android:layout_height Height of the layoutYou can given width as like below,

–          fill_parent : Covers all the areas of parent view width wise.

–          wrap_content : Covers width based on your content’s size.

–          match_parent : Contains as much width as its parent has.

Ex. android:layout_height=”wrap_content”

Or you can specify int value as well, which is counted as dip value. (Dimensions In Pixels)

Ex. android:layout_width=”200dip”

android:layout_weight Specifies how much extra space should be allocated to the view.You can provided the value in a friction number or int value. It will be considered as a percent value.

Ex. android:layout_weight=”50”

Considered as 50% of the total space.

Ex. android:layout_weight=”0.1”

Considered as 10% value out of 100%

Ex. android:layout_weight = “1”

This will be considered as remaining whole space of the view group will be occupied by this view.

android:orientation Defines the manner of the children.

–          horizontal

–          vertical

Ex. android:orientation = “vertical”

android:layout_gravity Specifies how the layout will be positioned.There are multiple options available, basic and general used options are,

–          top : Top side of the parent view.

–          bottom : Bottom side of the parent view.

–          left : Left side of the parent view.

–          right : Right side of the parent view.

–          center : Center of the parent view from horizontally as well vertically.

–          center_verticle : Centered vertically of the parent view.

–          center_horizontal : Centered horizontally of the parent view.

Ex. android:layout_gravity = “center”

android:gravity Specifies how the child views will be positioned.There are multiple options available, basic and general used options are,

–          top : Top side of the layout.

–          bottom : Bottom side of the layout.

–          left : Left side of the layout.

–          right : Right side of the layout.

–          center : Center of the layout from horizontally as well vertically.

–          center_verticle : Centered vertically of the layout.

–          center_horizontal : Centered horizontally of the layout.

Ex. android:gravity = “top”

android:layout_margin Keeping extra space to each side of the layout.You can give any int value to this field. It will be considered as dip value.

Ex. android:layout_margin = “10dip”

android:layout_marginTop Keeping extra space to the Top side of the layout.You can give any int value to this field. It will be considered as dip value.

Ex. android:layout_marginTop = “10dip”

android:layout_marginBottom Keeping extra space to the Bottom side of the layout.You can give any int value to this field. It will be considered as dip value.

Ex. android:layout_marginBottom = “10dip”

android:layout_marginRight Keeping extra space to the Right side of the layout.You can give any int value to this field. It will be considered as dip value.

Ex. android:layout_marginRight = “10dip”

android:layout_marginLeft Keeping extra space to the Left side of the layout.You can give any int value to this field. It will be considered as dip value.

Ex. android:layout_marginLeft = “10dip”

android:padding Padding filled to each side of the Layout.You can give any int value to this field. It will be considered as dip value.

Ex. android:padding = “10dip”

android:paddingTop Padding Top filled to each side of the Layout.You can give any int value to this field. It will be considered as dip value.

Ex. android:paddingTop = “10dip”

android:paddingBottom Padding Bottom filled to each side of the Layout.You can give any int value to this field. It will be considered as dip value.

Ex. android:paddingBottom = “10dip”

android:paddingRight Padding Right filled to each side of the Layout.You can give any int value to this field. It will be considered as dip value.

Ex. android:paddingRight = “10dip”

android:paddingLeft Padding Left filled to each side of the Layout.You can give any int value to this field. It will be considered as dip value.

Ex. android:paddingLeft = “10dip”

android:background Specifying background image or the color to the layout.You can specify here a drawable image or can provide color too.

Ex.

android:background = “@drawable/img.jpg” or

android:background = “@color/red” or

android:background = “#FF00FF”

android:visibility Defined the visibility of the layout as well its contained children.It contains the value such that whether you want to show the layout and its children or need to invisible it.The values are,

–          visible : Visible to the end user

–          invisible : Invisible to the end user

–          gone : Remove from the screen.

Ex. android:visibility = “gone”

How to use ?

You can use these attributes in the xml like as illustrated below :

<LinearLayout

android:id=<i>"@+id/ll_demo"</i>

android:layout_width=<i>"fill_parent"</i>

android:layout_height=<i>"fill_parent"</i>

android:layout_weight=<i>"1"</i>

android:orientation=<i>"vertical"</i>

          android:gravity=<i>"center"</i>

          android:layout_marginLeft=<i>"10dip"</i>

          android:layout_marginRight=<i>"10dip"</i>

          android:padding=<i>"10dip”</i>

          android:background=<i>"#ff0000"</i>&gt;

You can get the output like,

screen_XML

Note : Even you can add the Layout run time as well using programmatically.

Step 1 : Define LinearLayout

LinearLayoutll_new = new LinearLayout(this);

Step 2 : Define the size of the layout using LayoutParams

LayoutParamsll_param = <b>new</b>LinearLayout.LayoutParams(LayoutParams.<i>FILL_PARENT</i>, LayoutParams.<i>FILL_PARENT</i>, 1);

Step 3 : Assigning LayoutParams to the LinearLayout

ll_new.setLayoutParams(ll_param);

Step 4 : Set properties of layout, if you want

You can set any property here, as we were defined using XML file.

ll_new.setGravity(Gravity.<i>CENTER</i>);

ll_new.setBackgroundColor(Color.<i>WHITE</i>);

Step 5 : Defining any Widget/View

You can define any view as here we are defining Button View.

 Button button = <b>new</b>Button(<b>this</b>);

button.setText("Button B3");

button.setTextSize(20f);

Step 6 : Adding View to the as a LinearLayout child

ll_new.addView(button);

Step 7 : Setting the Content View

Now at the last step you need to set the content to display the Layout and its under coming objects to the screen. And it will be declared at the end of the onCreate() method.

 setContentView(ll_new);

Step 8 : Output

And you will get output like.

For getting the clear idea you can find the source code of LinearLayout using Java code here.

If you have any query, feel free to comment..!!

You may also like