0

When i check the design for this code. this WeightSum act totally opposite the way i want. when i set my button weightSum for 70 it takes 30 (total weightSum is 100) vise-versa.

  <LinearLayout
         android:weightSum="100"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         >

             <Button
                 android:layout_weight="70"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="Button" />

             <ToggleButton
                 android:layout_weight="30"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="ToggleButton" />


    </LinearLayout>
  • Check this link :- https://stackoverflow.com/questions/7452741/what-is-androidweightsum-in-android-and-how-does-it-work – Abhinav Gupta Feb 22 '18 at 10:25

5 Answers5

4

So android:weightSum defines the maximum weight sum of Layout, and it is calculate total sum of the layout_weight of all the its children views.

Example:- a LinearLayout having 3 Views(Which can be anything). Now you want to show 3 views equally in screen. So need to put layout_weight to views 1 and your weightSum is 3.

<LinearLayout
         android:weightSum="100"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         >

             <View
                 android:layout_weight="70"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="Button" />

             <View
                 android:layout_weight="30"
                 android:layout_width="fill_parent"
                 android:layout_height="0dp`enter code here`"
                 android:text="ToggleButton" />


    </LinearLayout>

or You can also put your android:layout_weight in points also like below :-

<LinearLayout
         android:weightSum="1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         >

             <View
                 android:layout_weight=".7"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="Button" />

             <View
                 android:layout_weight=".3"
                 android:layout_width="fill_parent"
                 android:layout_height="0dp`enter code here`"
                 android:text="ToggleButton" />


    </LinearLayout>

Remember 3 thing before use android:weightSum :-

  1. set the android:layout_width of the children to "0dp"

  2. set the android:weightSum of the parent (edit: as Jason Moore noticed, this attribute is optional, because by default it is set to the children's layout_weight sum)

  3. set the android:layout_weight of each child proportionally (e.g. weightSum="5", three children: layout_weight="1", layout_weight="3", layout_weight="1")

duggu
  • 37,851
  • 12
  • 116
  • 113
3

You need set android:layout_width="0dp" of Button and ToggleButton.

Tung Tran
  • 2,885
  • 2
  • 17
  • 24
1

you have set children view's width=0 if LinearLayout orientation is horizontal and height = 0 if orientation is vertical I have made changes to your code, please refer :)

 <LinearLayout
         android:weightSum="100"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal">

             <Button
                 android:layout_weight="70"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="Button" />

             <ToggleButton
                 android:layout_weight="30"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="ToggleButton" />


    </LinearLayout>

It works on percentage basis, When you want to make your views' height and width to occupy certain percentage of its parent then weights is the solution e.g. if weightSum = 1 children can be of weight_layout .30 and.60. So basically addition of children's' layout_weight should be equals to weightSum of their parent (or less in WRT use cases)

NotABot
  • 781
  • 7
  • 24
0

based orientation, you should set layout_height or layout_width of parent layout to 0dp for correct behavior of weightsum.

Nikunj
  • 3,937
  • 19
  • 33
0

In order to make it work well, as others have pointed, the width (or height, if the LinearLayout orientation was vertical) should be 0 (px or dp, whatever you wish, as it's still 0) for the children that have a weight.

About explanation of how it works, if not all had weight, the remaining one would be spread to them.

In your case, you don't have to set weightSum at all, because you've already set the weight for each of them (70+30 is indeed 100).

What it will do (after the needed changes), is set the first View to have a width that's 30% of the width of the parent, and the second view with 70% of the width of the parent.

android developer
  • 114,585
  • 152
  • 739
  • 1,270