Linear Layout and weight in Android
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
LinearLayout arranges child views in a single row or a single column. The layout_weight attribute tells Android how to divide the remaining space along that layout direction, which makes it useful for building rows and columns that adapt to different screen sizes.
How Weight Actually Works
Weight applies only along the LinearLayout orientation:
- in a horizontal layout, weight controls width distribution
- in a vertical layout, weight controls height distribution
Android first measures any fixed-size children, then divides the remaining space among weighted children according to their relative weight values.
Here is a common horizontal example with two buttons sharing the row equally:
Setting layout_width to 0dp is important here. It tells the layout not to reserve intrinsic width before weight is applied. The same pattern applies vertically with layout_height="0dp".
Mix Fixed and Weighted Children Carefully
Weighted and fixed-size children can live in the same container. The fixed-size views take their space first, and the weighted views divide what remains.
This is a common pattern for list rows, cards, and settings screens where an icon has fixed width and text expands.
weightSum Is Optional in Most Cases
You can set android:weightSum on the parent, but it is usually unnecessary. If you omit it, Android uses the total of the child weights automatically.
For example, weights 1 and 2 divide space into one-third and two-thirds. Explicit weightSum is mainly useful when you want a fixed denominator for clarity or when some part of the weight system is intentionally left unused.
The same idea works in vertical layouts. If three views each have layout_height="0dp" and weight 1, they divide the parent's available height equally. That is a simple way to build dashboards, stacked action panels, or onboarding screens with evenly shared sections.
Use Weight Where It Helps, Not Everywhere
LinearLayout with weight is great for simple adaptive rows and columns, but very deep nesting can become hard to maintain. For more complex relationships, ConstraintLayout is often a better choice because it expresses alignment rules without stacking many container views.
That does not make weight obsolete. It is still one of the clearest tools for straightforward proportional layouts.
In practice, many Android screens still mix both approaches: ConstraintLayout for the overall page and a small LinearLayout with weights for a row that needs simple proportional behavior.
Common Pitfalls
- Forgetting to set the weighted dimension to
0dp, which often leads to confusing sizing. - Expecting weight to affect the cross-axis dimension. In a horizontal layout, it does not control height.
- Over-nesting
LinearLayoutcontainers when a simpler parent layout would be easier to maintain. - Mixing large margins, padding, and weights without testing on small screens.
- Assigning weights to every child by default, even when only one flexible view is needed to absorb the remaining space.
Summary
- '
layout_weightdivides remaining space along theLinearLayoutorientation.' - Use
0dpon the weighted dimension so weight controls the size cleanly. - Fixed-size and weighted children can be combined in the same layout.
- '
weightSumis optional unless you need explicit control over the total weight base.' - Weight is best for simple proportional layouts, not for every complex screen.

