Setting Layout Weight Programmatically

01 September 2013 - Filed under android

What is layout_weight?

Layout weights are a very useful layout mechanism to help create UIs that scale more easily. The Android Developer Guide says the following:

layout_weight is used in LinearLayouts to assign importance to Views within the layout.

All Views have a default layout_weight of zero, meaning they take up only as much room on the screen as they need to be displayed.

Assigning a value higher than zero will split up the rest of the available space in the parent View, according to the value of each View's layout_weight and its ratio to the overall layout_weight specified in the current layout for this and other View elements. This means you can easily have items in your linear layouts take up the same ratio of space as the layout width or height scales.

Setting layout_weight in XML

Setting layout weight in XML is simple: set the android:layout_weight proeprty to the desired ratio, and depending on if you want to scale width/height, set the layout_width/layout_height property to 0dip


    <!-- android:layout_weight indicates the relative ratio -->
    <!-- 0dip forces scaling to match the weighted value -->

    <!-- 1st RelativeLayout will take up 2/3 of the parent's height -->
    <!-- 2nd RelativeLayout will take up 1/3 of the parent's height -->

Setting layout_weight in code (programmatically)

While still easy to do, setting layout weight in code catch you (as it did me) up in a few places.

// Main linear layout; will contain the slider and and value
LinearLayout mainLayout = new LinearLayout(ctx);

// Create first inner layout
RelativeLayout innerLayout1 = new RelativeLayout(ctx);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 2f);  

// Create first inner layout
RelativeLayout innerLayout2 = new RelativeLayout(ctx);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f);  

// Add new layouts to main, linear layout

A few things to note:

  1. We use LinearLayout.LayoutParams even though we are setting up the parameters on a RelativeLayout. LayoutParams are in relation to the parent layout, which in our case is a LinearLayout.
  2. 0dip not used in the LayoutParam object; the orientation of the layout will indicate our scaling ratio; in our example we have a VERTICAL orientation, so the height will be the weighted property.
  3. Make sure to cast the weighted value to a float (1f).
Watch: Any class that extends LinearLayout may have weighted children. When setting in code, be sure to use the extended parent class, not LinearLayout. Failing to do so will cause extremely confusing run time errors (I know from experience!)

For example, if you have a RadioGroup and you are setting your RadioButton weight use:

RadioGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1.0f));

by Shayla Sawchenko