×

Linear Layout


What is Linear Layout?

Linear Layout is used to align the UI elements either horizontally or vertically. It will be only in one direction i.e horizontal or vertical. Linear Layout looks like this , bars here represents UI element like Label,Button,EditText ec.

You can specify the orientation with android:orientation attribute like this,

android:orientation="vertical"
             or
android:orientation="horizontal"

Linear layout in vertical orientation


Linear layout in horizontal orientation


Example :-


<?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="12dp"
    android:paddingRight="12dp"
    android:orientation="vertical" >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password" />

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="login" />
</LinearLayout>

We discussed in detail about match_parent and wrap_cotent in previous tutorial. paddingLeft and paddingRight are used to specify the left & right position of view. EditText is used to create the textfield and you can assign placeholder using android:hint attribute. Button is used to create the Button and you can assign layout gravity to place the button in left,right,center direction.

Output :-