Table of Contents
Overview
LinearLayout is one of the simplest and most commonly used layout containers in Android. It arranges its child views in a single direction, either all in a vertical column or all in a horizontal row. Understanding LinearLayout is useful both for creating simple screens and for reading existing Android code, because many sample projects and older apps use it extensively.
In this chapter you will learn how LinearLayout decides where to place its children, how layout_width and layout_height work inside it, what layout_weight does, and how to control spacing and alignment. You will not learn XML basics here, only what is specific to LinearLayout itself.
Orientation
The defining property of a LinearLayout is its orientation attribute. This tells Android whether child views are stacked on top of each other or placed next to each other.
A LinearLayout with vertical orientation arranges children from top to bottom. A LinearLayout with horizontal orientation arranges children from left to right, or right to left when using right to left layouts, but the concept stays the same.
Example of a vertical LinearLayout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second" />
</LinearLayout>
In this example, the TextView views will appear one below the other.
Example of a horizontal LinearLayout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Left"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Right"
android:layout_weight="1" />
</LinearLayout>In this horizontal example, the buttons will appear side by side. How they share space will be explained in the weight section.
In a LinearLayout, always set android:orientation. If you forget it, the layout may not behave as you expect, and tools or future code readers may be confused.
Layout Parameters inside LinearLayout
Every child view inside a LinearLayout must specify android:layout_width and android:layout_height. These attributes tell the parent how big the child wants to be.
Inside LinearLayout, there are three common values for width or height.
wrap_content means the view wants to be just big enough to show its content. For a TextView, this is usually the size of the text plus some padding.
match_parent means the view wants to be as large as the parent in that dimension.
0dp is a special value inside LinearLayout when used with layout_weight. It tells the view to give up controlling its own size in that dimension, and let weight decide instead.
Which dimension is “main” depends on the orientation. In a vertical LinearLayout, the main dimension is height. In a horizontal LinearLayout, the main dimension is width.
As a general guideline, for the main dimension use wrap_content, match_parent, or 0dp with weight. For the cross dimension, which is perpendicular to orientation, use wrap_content or match_parent, but not 0dp for normal cases.
Using layout_weight
The android:layout_weight attribute is unique to LinearLayout and is one of its most powerful features. It lets children share extra space proportionally.
Imagine you have remaining free space in the main direction of the layout. We call this “remaining space” the extra space. Weight tells the LinearLayout how to divide this extra space among the children.
Suppose in a horizontal LinearLayout you have three children, and their weights are 1, 2, and 1. The total weight is:
$$
\text{total\_weight} = 1 + 2 + 1 = 4
$$
The extra space is divided into 4 equal parts. The first and third children each get 1 part. The middle child gets 2 parts. The result is that the middle child occupies half of the extra space, and the other two share the remaining half.
When using layout_weight in the main orientation, you should usually set the size in that dimension to 0dp. For example, in a horizontal LinearLayout set android:layout_width="0dp" on weighted children. This prevents content size from interfering with weight calculations.
A typical horizontal example with weights:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="25%" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="50%" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="25%" />
</LinearLayout>If the total width available after measuring the height is $W$ and the space used by non weighted children is $U$, then the extra width is:
$$
\text{extraWidth} = W - U
$$
Each child with weight $w_i$ receives:
$$
\text{childExtra} = \text{extraWidth} \times \frac{w_i}{\text{totalWeight}}
$$
where totalWeight is the sum of all weights.
You do not need to calculate these values in real code, the layout system does it automatically, but keeping this proportional relation in mind helps you predict how the UI will look.
You can also use weight with vertical LinearLayouts. For example, you can split the screen between a list and a footer:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Footer" />
</LinearLayout>
In this case the ListView grows to fill all remaining height above the footer.
Gravity and Layout Gravity
LinearLayout supports two related but different concepts that control positioning: android:gravity on the parent and android:layout_gravity on the child.
android:gravity on the LinearLayout controls how child views are placed when there is extra space in the cross or main direction, depending on configuration. For example, you can align children to the center horizontally or vertically inside the LinearLayout.
Example with centered children in a vertical LinearLayout:
<LinearLayout
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="Centered text" />
</LinearLayout>
Here the TextView will appear in the exact center of the parent in both directions.
android:layout_gravity is set on individual children and tells the parent how that particular child wants to be positioned inside any extra space assigned to it. For LinearLayout this is most noticeable when the parent does not use weights and not all children fill the cross dimension.
Example with layout_gravity:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top"
android:layout_gravity="top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom"
android:layout_gravity="bottom" />
</LinearLayout>
In a horizontal LinearLayout the height can be larger than the children. With layout_gravity set, one child sticks to the top and the other to the bottom of the parent height.
Remember that gravity belongs to the container and affects all children by default. layout_gravity belongs to the child and affects only that view.
Controlling Spacing: Padding and Margins
LinearLayout does not automatically create spacing between children. You control spacing with margins on children and padding on the parent or the child.
Padding is inside the view border. It pushes the content inward. For a LinearLayout, padding increases the space between the parent’s edges and its children.
Margin is outside the view border. It pushes the view away from other views or the parent’s edges.
In a vertical LinearLayout, you can add vertical spacing between items by giving the children android:layout_marginTop or android:layout_marginBottom. In a horizontal LinearLayout, use layout_marginStart or layout_marginEnd or layout_marginLeft and layout_marginRight depending on your target configuration.
Example:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item 1"
android:layout_marginBottom="8dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item 2" />
</LinearLayout>In this example, the first item has space under it because of the bottom margin. The whole group has space around it because of the parent padding.
When you work on responsive layouts you will often move margin and padding values into resource files, but the basic behavior stays the same.
WeightSum and Fill Behavior
By default, LinearLayout computes the total weight as the sum of all children’s layout_weight values. You can override this with the android:weightSum attribute on the parent. In practice, you rarely need to do so.
If you set android:weightSum, the layout uses that as the denominator in the weight fraction instead of computing a sum. This can be used if you want a constant total weight even when some children have dynamic weights, but it can also introduce visual confusion.
A more common task is to control how children behave when they have both a measured size and a weight. LinearLayout has an attribute called measureWithLargestChild (in the support library it may appear as android:measureWithLargestChild). When this is true, the largest child in the main orientation can influence the size of all children. This is useful when you want buttons in a row to share the size of the biggest one, but for a beginner project, leaving it as the default false is usually best.
When you set a child’s layout_width or layout_height to wrap_content along with a positive layout_weight, LinearLayout first measures the child’s natural size, then it may still allocate extra space based on weight. This can lead to uneven results if some views are larger by content. To get predictable proportional splits, use 0dp in the main dimension.
Nested LinearLayouts
You can put one LinearLayout inside another to build more complex structures. For example, you can have a vertical LinearLayout where each row is itself a horizontal LinearLayout.
Example:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Label" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>This creates a form with a row that splits space between label and input, and a button under it.
Too many nested LinearLayouts can make layout measurement slow and make your XML hard to read. For complex arrangements you will usually prefer ConstraintLayout, which reduces the need for nesting. Still, nested LinearLayouts are fine for simple screens or small sections of a layout.
When to Use LinearLayout
LinearLayout is useful when you have a simple list of elements in one direction, such as a column of text fields and buttons, or a row of icons. It is easy to reason about, and weight provides a simple way to divide space.
Use a vertical LinearLayout for stacked sections: headers, content blocks, and footers. Use a horizontal LinearLayout for simple toolbars, button rows, and evenly spaced items.
When you find yourself adding many nested LinearLayouts to achieve complex grids or overlapping positions, that is often a sign that a different layout type would be better. In this course, ConstraintLayout will cover those more advanced cases. LinearLayout remains a good option for straightforward one dimensional layouts where simplicity is more important than flexibility.