Android Linear Layout

A linear layout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android: orientation attribute.

Vertical: In this, all the children are arranged vertically in a line one after the other.

Horizontal: In this, all the children are arranged horizontally in a line one after the other.

Android Linear Layout 

LinearLayout Attributes

Following are the important attributes specific to LinearLayout −

Sr.No Attribute & Description
1

android:id

This is the ID which uniquely identifies the layout.

2

android:baselineAligned

This must be a boolean value, either "true" or "false" and prevents the layout from aligning its children's baselines.

3

android:baselineAlignedChildIndex

When a linear layout is part of another layout that is baseline aligned, it can specify which of its children to baseline align.

4

android:divider

This is drawable to use as a vertical divider between buttons. You use a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

5

android:gravity

This specifies how an object should position its content, on both the X and Y axes. Possible values are top, bottom, left, right, center, center_vertical, center_horizontal etc.

6

android:orientation

This specifies the direction of arrangement and you will use "horizontal" for a row, "vertical" for a column. The default is horizontal.

7

android:weightSum

Sum up of child weight

Example

This example will take you through simple steps to show how to create your own Android application using Linear Layout. Follow the following steps to modify the Android application we created in Hello World Example chapter −

Step Description
1 You will use Android Studio to create an Android application with empty activity.
2 Modify the default content of res/layout/activity_main.xml file use to code provided below.
3 Run the application to launch the Android emulator and check the output

Following is the content of the modified main activity file src/com.example.demo/MainActivity.java. This file can include each of the fundamental lifecycle methods.

package com.example.demo;

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

public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
}

Following will be the content of res/layout/activity_main.xml file:

<?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:orientation="vertical"
    android:gravity="center">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="H O R I Z O N T A L  L A Y O U T"
    android:layout_gravity="left"
    android:layout_marginLeft="10dp"
    android:textSize="12sp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hori"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hori"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hori"/>

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="V E R T I C A L  L A Y O U T"
        android:layout_gravity="left"
        android:layout_marginLeft="10dp"
        android:textSize="12sp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Vertical"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Vertical"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Vertical"/>

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="G R I D  L A Y O U T"
        android:layout_gravity="left"
        android:layout_marginLeft="10dp"
        android:textSize="12sp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Grid"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Grid"/>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Grid"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Grid"/>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

Let's try to run the application we just created. I assume you had created your AVD while doing the environment setup or using an android device in debugging mode. To run the app from Android studio, open one of your project's activity files, and click Run  Eclipse Run Icon  icon from the toolbar. Android Studio installs the app on your connected device/emulator and starts it and if everything is fine with your setup and application, it will display the following screen.

Android Linear Layout Screen



×