Android Linear Layout with Example

By | September 7, 2012
 In a linear layout, as the name suggests, all the elements or Views (like TextView , button ,…) inside this layout are displayed in a linear fashion, either Horizontally or Vertically and this behavior is set in android: orientation which is an attribute of the node Linear Layout.

Layout Directory

Android allows you to create view layouts using simple XML file ,All the layouts must be placed in /res/layout  directory.

Linear Layout Directory Structure

1. Linear Layout-Horizontal

In case of horizontal placement of elements in linear layout set the property to attribute android:setOrientation=”horizontal” inside Linear Layout node

      
file:-linear_layout_hor.xml
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    xmlns:tools=”http://schemas.android.com/tools”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”horizontal” >

    <EditText
        android:id=”@+id/edittext1″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:hint=”Enter The Text”
        android:inputType=”text” />

    <Button
        android:id=”@+id/button1″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:text=”Go” />

    <Button
        android:id=”@+id/button2″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:text=”Cancel” />

</LinearLayout>

Create Activity Class
 
file:-Linear_layout_hor.java
package com.example.linear_layout;
import android.os.Bundle;
import android.app.Activity;

public class Linear_layout_hor extends Activity {

    @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.linear_layout_hor);
    }
}

 RUN

Android Linear Layout Horizontal


LinearLayout – horizontal.Zip

2. Linear Layout – Vertical

In case of vertical placement of elements in linear layout set the property to android: setOrientation =”vertical” inside Linear Layout node .

Create XML File :-

file: linear_layout_ver.xml

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    xmlns:tools=”http://schemas.android.com/tools”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”vertical” >

    <EditText
        android:id=”@+id/edittext1″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:hint=”Enter The Text”
        android:inputType=”text” />

    <Button
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:id=”@+id/button1″
        android:text=”Go” />

    <Button
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:id=”@+id/button2″
        android:text=”Cancel” />

</LinearLayout>

Create Activity Class
 
file : Linear_layout_var.java
package com.example.linearlayout_vertical;

import android.os.Bundle;
import android.app.Activity;

public class Linear_layout_var extends Activity {

    @Override
     public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.linear_layout_ver);
   }
}

RUN

Android Linear Layout Vertical
 LinearLayout-vertical.Zip