What Is Android:Weightsum in Android, and How Does It Work

What is android:weightSum in android, and how does it work?

Per documentation, android:weightSum defines the maximum weight sum, and is calculated as the sum of the layout_weight of all the children if not specified explicitly.

Let's consider an example with a LinearLayout with horizontal orientation and 3 ImageViews inside it. Now we want these ImageViews always to take equal space. To acheive this, you can set the layout_weight of each ImageView to 1 and the weightSum will be calculated to be equal to 3 as shown in the comment.

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- android:weightSum="3" -->
android:orientation="horizontal"
android:layout_gravity="center">

<ImageView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"/>
.....

weightSum is useful for having the layout rendered correctly for any device, which will not happen if you set width and height directly.

Can someone explain how this weightsum work? and why

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")

Android Layout - layoutweight and weightsum

Use this XML file. I have made the changes for you.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:weightSum="100" >

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

<!-- Below is the first modification to layout_width -->

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="40"
android:orientation="vertical"
android:weightSum="235" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="100"
android:background="#ff0000"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="45"
android:background="#ffff00"
android:orientation="vertical" >

<ViewFlipper
android:id="@+id/view_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tvItemName" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ViewFlipper>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="90"
android:background="#ffffff"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

<!-- Below is the second modification to layout_width -->

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60"
android:orientation="vertical"
android:weightSum="100" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="30"
android:background="#00ab00"
android:orientation="vertical"
android:weightSum="100" >

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="Test data Test dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest data"
android:textColor="#000000" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="70"
android:background="#cd00ab"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
android:background="#ab0000" >
</LinearLayout>

</LinearLayout>

It looks fine to me. check out the below snapshot.

Layout snapshot with little text
Layout snapshot with more text

WeightSum not working as expected in a LinearLayout in android

You should set android:orientation="horizontal" in your XML code .

And add android:layout_width="0dp" in your Button and EditText .

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

<EditText
android:id="@+id/edit1"
android:inputType="textAutoComplete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/DBLocations"
android:layout_weight="1"/>
</LinearLayout>

Why use android:weightSum?

The important word in the reference documentation description is "single": "This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0." In this case, the sum of the weights of the children (in this case, only child) is different to the weightSum.

So you only need to use weightSum when you won't necessarily have the children filling the entire LinearLayout.

Android - Vertical and Horizontal WeightSum

Instead of using LinearLayout, it's much easier to accomplish what you want using a GridLayout. Here's a simple example on how to place 4 images in a quadrants manner:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:rowCount="2"
android:orientation="horizontal"
tools:context=".GridXMLActivity">
<ImageView
android:id="@+id/image1"
android:layout_width="0dp"
android:src="@drawable/Sun3"
android:layout_columnWeight="1"
android:adjustViewBounds="true"
/>

<ImageView
android:id="@+id/image2"
android:layout_width="0dp"
android:src="@drawable/Sun3"
android:layout_columnWeight="1"
android:adjustViewBounds="true"
/>

<ImageView
android:id="@+id/image3"
android:layout_width="0dp"
android:src="@drawable/Sun3"
android:layout_columnWeight="1"
android:adjustViewBounds="true"/>

<ImageView
android:id="@+id/image4"
android:layout_width="0dp"
android:src="@drawable/Sun3"
android:layout_columnWeight="1"
android:adjustViewBounds="true"/>
</GridLayout>

Hope it helps.



Related Topics



Leave a reply



Submit