Android Development
Layout Weight
Dynamic Layouts
Android Programming
Mobile App Development

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:

kotlin
1val view = findViewById<View>(R.id.panel)
2val params = view.layoutParams as LinearLayout.LayoutParams
3
4params.width = 0
5params.weight = 2f
6
7view.layoutParams = params

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:

java
1View view = findViewById(R.id.panel);
2LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
3
4params.width = 0;
5params.weight = 2f;
6
7view.setLayoutParams(params);

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.

kotlin
1val leftParams = leftView.layoutParams as LinearLayout.LayoutParams
2leftParams.width = 0
3leftParams.weight = 1f
4leftView.layoutParams = leftParams
5
6val rightParams = rightView.layoutParams as LinearLayout.LayoutParams
7rightParams.width = 0
8rightParams.weight = 2f
9rightView.layoutParams = rightParams

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:

kotlin
view.requestLayout()

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.LayoutParams to change layout_weight from code.
  • Weight only works for direct children of a LinearLayout.
  • Set width or height to 0 on 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.

Course illustration
Course illustration

All Rights Reserved.