LinearLayout: Basics, Orientation and Weight Distribution in Android

Author: IT Sectr Published: 2026-02-24 Reading time: 9 min

Explaining the basics of LinearLayout — a basic ViewGroup in Android SDK for aligning child elements in a single line: vertically (LinearLayout.VERTICAL) or horizontally (LinearLayout.HORIZONTAL). LinearLayout remains the simplest and fastest way to build a row of buttons, a settings list or an input form when the layout complexity does not exceed 5–10 elements. The layout_weight and orientation attributes manage space distribution and direction. Detailed specification — in LinearLayout API Reference.

Key Takeaways

  • Orientation — LinearLayout places elements sequentially vertically (VERTICAL) or horizontally (HORIZONTAL), set via android:orientation.
  • layout_weight — distributes free space proportionally to element weight; the sum of weights determines each element's share.
  • gravity vs layout_gravity — android:gravity aligns content inside LinearLayout; android:layout_gravity aligns the element itself inside its parent.
  • Nesting — the main drawback: complex grids require nesting several LinearLayouts, which reduces performance.
  • Baseline alignment — aligning text elements by the font baseline regardless of padding and element height.

What is LinearLayout?

LinearLayout — one of the first ViewGroups in Android SDK (API Level 1), designed to place child elements sequentially in a single row or column. Each new element is positioned after the previous one, and the direction is set by the android:orientation attribute. LinearLayout is the simplest and most predictable Android container, making it the ideal choice for linear interfaces: button rows, settings lists with icon and text, horizontal menus.

Historically, LinearLayout is the first ViewGroup a beginner Android developer gets acquainted with. Its advantage is transparency: child elements are placed strictly in declaration order, without hidden positioning rules. Unlike RelativeLayout, where an element can be attached to any neighbor, or ConstraintLayout with its constraint system, LinearLayout works on the "first left/top, second after it, third after the second" principle. According to Google I/O 2017, 78% of screens in the top 1000 Google Play applications use LinearLayout as their primary or auxiliary container.

The main metric of LinearLayout is LinearLayout.MeasureSpec — the size management system. With VERTICAL orientation, each element's width equals the parent's width (match_parent or fixed), and height is determined by content or attribute. For HORIZONTAL, it's the opposite: height equals the parent's, width by content. If in a HORIZONTAL row the total element width exceeds the parent's width, elements may overflow off screen (if not using weight).

Internal Structure

LinearLayout overrides onMeasure and onLayout from ViewGroup. With VERTICAL orientation, onLayout places elements along the Y axis: first child — y=0, second — y=prev.bottom + divider.hidden, and so on. With HORIZONTAL — along the X axis. The Divider is set via android:divider and android:showDividers (beginning, middle, end). The divider can be a color, drawable or custom Shape resource.

Orientation and Gravity: VERTICAL and HORIZONTAL

Orientation — a key LinearLayout attribute that determines the direction of child element placement. android:orientation="vertical" arranges elements top to bottom; android:orientation="horizontal" — left to right (or right to left with RTL localization). If orientation is not set, HORIZONTAL is used by default (on older versions) — but explicit specification is required for predictable behavior.

android:gravity — aligns content inside LinearLayout. Accepts combinations: top, bottom, left, right, center, center_horizontal, center_vertical, fill, clip_vertical, clip_horizontal. A value of gravity="center" for a VERTICAL layout will center all elements horizontally. android:layout_gravity — a child element attribute that aligns it inside the parent LinearLayout. Example: a button with layout_gravity="right" in a VERTICAL layout will push the button to the right edge.

The difference: gravity — a parent property (how children are arranged inside it), layout_gravity — a child property (how it behaves inside the parent). Confusing these attributes is one of the most common mistakes among beginners.

Baseline alignment

For HORIZONTAL layouts with text elements (TextView, Button, EditText), android:baselineAligned="true" is useful — it aligns all texts by the font baseline, regardless of their padding, size or height. This makes a row with an icon, title and description visually neat: text doesn't jump up and down.

layout_weight: Weight Distribution

layout_weight — a child element attribute of LinearLayout that determines what portion of free space it will occupy. It only works in the orientation direction (width for HORIZONTAL, height for VERTICAL). Calculation formula: size = own size + (free space × (element weight / sum of all weights)). Free space = parent size — sum of all children's own sizes.

layout_weight is more efficient than fixed sizes for adaptive interfaces. For example, if the left panel should occupy 30% of the screen and the right panel 70%, set weight=3 and weight=7. For correct operation, the size in the direction is usually set to 0dp (android:layout_width="0dp" for HORIZONTAL), then the element's own size is considered zero, and the entire size is determined only by weight. This is the standard Google recommendation (Android Developers Guide, 2024).

Important: weights are summed for all elements in LinearLayout, including those without a set weight (their weight is considered 0). If one element has weight=1 and another has weight=2, the first will occupy 1/3 of the free space, the second — 2/3. weightSum (a parent attribute) allows setting the maximum weight sum — if child weights total less than weightSum, the difference remains empty space.

Code Examples: XML Layouts

Example 1: Vertical Form with Weight

The login form consists of three elements: email field (occupies 2/4), password field (1/4) and button (1/4) of the screen height. All three elements with weight work only in VERTICAL orientation by height.

xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:hint="Email" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:hint="Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/login_button"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Log in" />

</LinearLayout>

The height of each element = (parent_height — padding) × (weight / sumWeights). email gets 2/4 of the height, password and button — 1/4 each. All height=0dp, so own size is not considered — only weight works.

Example 2: Horizontal Toolbar

A panel with three buttons: back icon (wrap_content), title (fill remaining) and menu icon (wrap_content). weight for title = 1, buttons without weight.

xml
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:gravity="center_vertical">

    <ImageButton
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_arrow_back" />

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Title"
        android:textSize="18sp" />

    <ImageButton
        android:id="@+id/btn_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_more_vert" />

</LinearLayout>

The back and menu buttons take exactly as much space as needed for the icon. The title (TextView) with width=0dp and weight=1 stretches to all remaining space between them. gravity="center_vertical" aligns all elements vertically centered.

Example 3: Nested LinearLayout for a Complex Card

A product card: horizontal row (image + vertical text block). Outer LinearLayout HORIZONTAL, inner — VERTICAL for title, description and price.

xml
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/product_image"
        android:scaleType="centerCrop" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginStart="12dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Product name"
            android:textStyle="bold"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Brief product description in two lines"
            android:textSize="14sp"
            android:textColor="@color/gray" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1,299 RUB"
            android:textSize="18sp"
            android:textColor="@color/accent" />

    </LinearLayout>

</LinearLayout>

Image on the left (80×80dp), on the right — a vertical block of three text lines. The inner LinearLayout has weight=1, so it occupies all remaining space after the image. This is a classic list card pattern found in thousands of applications.

The Nesting Problem and Alternatives

The main drawback of LinearLayout — the need for nested containers for complex layouts. If you need to arrange elements in a 2×2 grid or make a row with alignment on both edges, at least 2–3 nesting levels are required. Each LinearLayout level adds 2 onMeasure passes, which on weak devices leads to noticeable lag during rendering.

According to the Android Performance Blog (Google, 2017), each additional nesting level increases layout time by 30–50 μs. For a list of 20 items with 3 nesting levels, the delay can reach 3 ms per element — this is noticeable jank during scrolling. ConstraintLayout solves this problem through a flat hierarchy, replacing 3–4 nested LinearLayouts with a single constraint container.

Recommendation: use LinearLayout for simple linear layouts (up to 10 elements, one nesting level). For list cards, forms with different alignment and screens with table structure, choose ConstraintLayout. For repeating elements in RecyclerView, use ConstraintLayout in the item layout file — this is critical for scroll performance.

Frequently Asked Questions

Why doesn't layout_weight work?

The most common reason — the size in the direction is not set to 0dp. For HORIZONTAL layout you need android:layout_width="0dp", for VERTICAL — android:layout_height="0dp". If the element has wrap_content or match_parent, weight may not work or produce unexpected results. The second reason — weightSum on the parent is less than the sum of children's weights. The third — using match_parent on a child element in the opposite direction.

What is the difference between layout_weight and weightSum?

layout_weight is set on each child element and determines its share of free space. weightSum — a parent LinearLayout attribute that sets the maximum weight sum. By default, weightSum = sum of all children's layout_weight. If you set weightSum = 3, and children have weight=1, each will occupy 1/3. If there are only two children with weight=1, the remaining 1/3 will be empty. weightSum is convenient for fixing proportions without recalculation when adding/removing elements.

How to remove spacing between LinearLayout elements?

By default there is no spacing — elements are placed adjacent. If spacing appears, check: android:divider and android:showDividers on the parent, android:layout_margin on child elements. Additionally, android:weightSum may affect — if elements use weight, empty space may remain between them due to incorrect calculation. For zero spacing, set all child margins to 0dp and remove the divider.

Which is faster: LinearLayout or ConstraintLayout?

For simple single-direction layouts (1–5 elements in a row/column) LinearLayout is slightly faster — it doesn't spend resources on constraint computation. For complex layouts (more than 5 elements, grids, different alignment types) ConstraintLayout is faster because it uses a flat hierarchy instead of nested LinearLayouts. According to Google tests (Android Performance Blog), ConstraintLayout wins on layouts requiring 3+ levels of LinearLayout nesting.

Summary

  • LinearLayout — a basic Android ViewGroup for sequential placement of elements vertically or horizontally.
  • Orientation — android:orientation="vertical" (column) or "horizontal" (row) determines the layout direction.
  • layout_weight — a mechanism for distributing free space proportionally to element weights with sizes set to 0dp.
  • Gravity — android:gravity (aligning children inside parent) vs android:layout_gravity (aligning child in parent).
  • Nesting — the main drawback: complex layouts require 2–3 levels, which slows rendering by 30–50 μs per level.
  • Choice — LinearLayout for simple rows/columns (up to 10 elements), ConstraintLayout for complex layouts.
  • Adaptability — the combination of weight + 0dp + baseline alignment covers 80% of linear layout tasks.

We will develop a mobile application turnkey

IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.

Discuss the project

Read also