Android LinearLayout

Introduction to LinearLayout

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. It is a commonly used view group that lays out its children views either horizontally or vertically.

The user can specify the layout direction with the android:orientation attribute.

By default the layout orientation is horizontal but all the controls are placed in single row or single column.

View Structure

View objects are the UI widgets such as buttons or text fields. These objects are responsible for handling the events. A LinearLayout respects margins between children and the gravity (right, left or center alignment) of each child.

view structure in android

The user can also specify the gravity by calling setGravity() which specifies the alignment of all the child element. The LinearLayout.LayoutParams class defines per-child layout information associated with ViewLinearLayout.

Layout Weight

  • The android:layout_weight attribute is used for assigning a weight to individual children.
  • The android:layout_weight assigns an “importance” value to a view in terms of how much space it should occupy on the screen.
  • A layout weight value allows the android:layout_weight attribute to expand and fill the remaining space in the parent view.
  • The default layout weight is zero.
  • Child view specifies a weight value and then any remaining space in the view group is assigned to children in the proportion of their declared weight.

LinearLayout XML Attributes

XML AttributesDescription
android:baselineAlignedWhen this attribute is set to false, the layout from aligning its children's baselines.
android:baselineAlignedChildIndexThis attribute specifies which of its children to baseline align.
Android:dividerThis attribute is used as a vertical divider between buttons.
android:gravityThis attribute specifies how an object should position its content, on both the X and Y axes, within its own bounds.
android:measureWithLargestChildThis attribute sets to true when all childrens with a weight will be considered having the minimum size of the largest child.
android:orientationThis attribute defines layout for column and row. Horizontal for a row and Vertical for a column.
android:weightSumThis attribute defines the maximum weight sum.

Example : LinearLayout with vertical and horizontal orientation

File Name: content_main.xml

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout
     xmlns:android = "http://schemas.android.com/apk/res/android"
     xmlns:tools = "http://schemas.android.com/tools"
     xmlns:app = "http://schemas.android.com/apk/res-auto"
     android:orientation = "vertical"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
     android:paddingLeft = "@dimen/activity_horizontal_margin"
     android:paddingRight = "@dimen/activity_horizontal_margin"
     android:paddingTop = "@dimen/activity_vertical_margin"
     android:paddingBottom = "@dimen/activity_vertical_margin"
     app:layout_behavior = "@string/appbar_scrolling_view_behavior"
     tools:showIn = "@layout/activity_main"
     tools:context = "com.example.itwin.demo1.MainActivity">

<Button
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:text = "Start"
     android:id = "@+id/button"
     android:layout_alignParentLeft = "true"
     android:layout_alignParentStart = "true"
     android:background = "#fff000"
     android:textSize = "25dp"
     />

<Button
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:text = "Stop"
     android:id = "@+id/button2"
     android:layout_below = "@+id/button"
     android:layout_alignParentLeft = "true"
     android:layout_alignParentStart = "true"
     android:background = "#0000ff"
     android:textSize = "25dp"
     />

<Button
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:text = "Pause"
     android:id = "@+id/button3"
     android:layout_below = "@+id/button2"
     android:layout_alignParentLeft = "true"
     android:layout_alignParentStart = "true"
     android:background = "#00f000"
     android:textSize = "25dp"
     />
</LinearLayout>


Output:

android:orientation=“vertical”

android orientation vertical

android:orientation=“horizontal”

andriod orientation horizontal