In this tutorial, we will provide an overview of android linear layout. We will also explore some of the attributes of linear layout and the type of orientation of linear layout.

Linear Layout in android

Linear Layout is a view group that aligns all children in a single direction, vertically or horizontally. We can specify the layout direction with the android:orientation attribute. This is a very widely-used layout for creating forms on Android. It is a layout that arranges its children i.e the various views and layouts linearly (one after another) in a single column(vertically) or a single row(horizontally). The arrangement of children will be horizontally or vertically depends upon the value of attribute android:orientation. By default orientation is horizontal.

Types of Orientation in Linear Layout

  1. Vertical Orientation :- The orientation attribute used to set the child/views horizontally or vertically.
Linear Layout Vertical
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
            android:id="@+id/button5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button1" />
        <Button
            android:id="@+id/button6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button2" />
        <Button
            android:id="@+id/button7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button3" />
        <Button
            android:id="@+id/button8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button4" />
</LinearLayout>
  • Horizontal Orientation :- The gravity attribute is an optional attribute which is used to control the alignment of the layout like left, right, center, top, bottom etc. The default orientation is horizontal.
Linear Layout Horizontal
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
            android:id="@+id/button5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button1" />
        <Button
            android:id="@+id/button6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button2" />
        <Button
            android:id="@+id/button7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button3" />
        <Button
            android:id="@+id/button8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button4" />
</LinearLayout>

Attributes of LinearLayout

AttributeDescription
android:idThis is the ID which uniquely identifies the layout.
android:layout_widthIt is used to define the width of the layout
android:layout_heightIt is used to define the height of layout
android:baselineAlignedThis must be a boolean value, either “true” or “false” and prevents the layout from aligning its children’s baselines.
android:baselineAlignedChildIndexWhen a linear layout is part of another layout that is baseline aligned, it can specify which of its children to baseline align.
android:dividerThis is drawable to use as a vertical divider between buttons. You use a color value, in the form of “#rgb”, “#argb”, “#rrggbb”, or “#aarrggbb”.
android:gravityThis 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.
android:orientationThis specifies the direction of arrangement and you will use “horizontal” for a row, “vertical” for a column. The default is horizontal.

Thank you 🙂

%d bloggers like this: