How to set layout_weight attribute dynamically from code?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To set layout_weight dynamically in Android, you update the child view’s LinearLayout.LayoutParams in code. The key detail is that weight only matters inside a LinearLayout, and the width or height often needs to be 0 on the weighted axis so the weight value can actually control space distribution.
How Weight Works in LinearLayout
layout_weight tells a LinearLayout how to divide remaining space among its direct children.
For a horizontal LinearLayout:
- weight affects width distribution
- the child width is often set to
0
For a vertical LinearLayout:
- weight affects height distribution
- the child height is often set to
0
If you change only the weight and leave the measured dimension fixed, the effect may not be what you expect.
Kotlin Example
Here is a simple Kotlin example that changes weight at runtime:
This assumes the parent is a horizontal LinearLayout. Setting width = 0 lets the weight take control of horizontal space.
Java Example
The same idea in Java:
If the parent layout is vertical, you would usually adjust height instead of width.
Example with Two Views Sharing Space
Suppose you want two horizontal panels where one takes one-third of the space and the other takes two-thirds.
The ratio comes from the relative weights, not from absolute percentages.
Parent Type Matters
A very common source of confusion is trying to set layout_weight on a child whose parent is not a LinearLayout.
If the parent is ConstraintLayout, RelativeLayout, or something else, LinearLayout.LayoutParams is the wrong type and weight will not apply.
So before changing weight dynamically, confirm the child is a direct child of a LinearLayout.
Triggering Layout Updates
In most cases, reassigning the layout params is enough. If the UI still does not refresh as expected, a layout request can help:
This is especially useful if several layout-related properties change together.
XML and Code Should Agree
You can define an initial weight in XML and change it later in code. But if the XML dimension conflicts with your runtime intent, the visual result may still look wrong.
For example, a weighted horizontal child that keeps layout_width="wrap_content" may not expand the way you expected. Often the fix is to switch that width to 0dp in XML or to 0 in code.
Common Pitfalls
The most common mistake is changing only weight and forgetting to set the weighted dimension to 0 on the relevant axis.
Another issue is casting to LinearLayout.LayoutParams when the parent is not actually a LinearLayout. That causes crashes or no visible effect.
People also forget that weight applies only to direct children of the LinearLayout, not to deeper descendants automatically.
Finally, do not assume weight works the same way in every layout system. ConstraintLayout has different mechanisms for distributing space.
Summary
- Update
LinearLayout.LayoutParamsto changelayout_weightfrom code. - Weight only works for direct children of a
LinearLayout. - Set width or height to
0on the weighted axis when appropriate. - Reassign the modified layout params back to the view.
- If nothing changes, check the parent type and whether the XML dimensions conflict with the intended weighting.

