0

Whether android:weightSum is mandatory or not to use android:layout_weight ? Can we use directly android:layout_weight inside a linearlayout without assign android:weightSum. Anyone please tell me pros and cons of using android:layout_weightand without android:weightSum. Thanks in advance.

Sample code:

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/table_view"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button_1"
                style="@style/BaseButtonStyle"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:text="@string/start" />

            <Button
                android:id="@+id/button_2"
                style="@style/BaseButtonStyle"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="1"
                android:text="@string/pause"
                android:textColor="@color/teal" />

            <Button
                android:id="@+id/button_3"
                style="@style/BaseButtonStyle"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:text="@string/stop" />
        </LinearLayout>
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58

2 Answers2

0

Can we use android:layout_weight without adding android:weightSum to parent ?

Yes, We can. Consider the example below -

Consider this example,

If, you want to design the row like Terms & Condition which is having TextView and ImageButton to the right. It will goes like:-

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:orientation="horizontal">

        <!-- Width of TextView = Width of LinearLayout - width of ImageButton -->
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?selectableItemBackgroundBorderless"
            android:src="@drawable/ic_vector_back_black_18dp"/>

    </LinearLayout>

For concept of WeightSum, You may refer this SO link

Community
  • 1
  • 1
Paresh P.
  • 6,677
  • 1
  • 14
  • 26
  • My doubt is LinearLayout doesn't set attribute weighSum. but children are having android:layout_weight with same or different weights. In your example only one child has android:layout_weight. for all child has weight where LinearLayout doesn't have weighSum. – Praveen Kumar Sugumaran May 22 '17 at 07:18
  • When you are using multiple weights, you must have to define `weightSum` to let the system measure - view height or width according to its parent (your case). While when you want to have one children to occupy whole the parent dimes, just use weight on that specific child, rest of child will occupy their dimen automatically(which is my case) . – Paresh P. May 22 '17 at 07:49
0

Yes we can use the weight without defining the weight sum in parent. IN that case the total weight of the child is calculated by adding the weight of each child.

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center">

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

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

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

The parent layout will now automatically take the weight sum as 3(sum of weight of each child)

Here are the more resources on this topic

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

and https://developer.android.com/reference/android/widget/LinearLayout.html

Community
  • 1
  • 1
Suresh Basnet
  • 147
  • 2
  • 13