1

I researched on another stackoverflow question//answer and found a working definition of layout_weight to be that "This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view" (source:What does android:layout_weight mean?)

I am trying to apply that concept to my code. Currently my code (without the layout weights) is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <TextView 
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Home"
       android:gravity="center"
     />
    <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="About"
       android:gravity="center"
      />
</LinearLayout>

And the current layout is https://i.stack.imgur.com/QTPGg.jpg (the home should take up all the space bc of match_parent)

However if i add android:layout_weight="1" to both text views, i get this layout https://i.stack.imgur.com/8y1Na.jpg

(Both are visible and share same amount of space)

My question is how is this happening from that working definition of layout_weight? layout_weight in this example will assign extra space equally to both the text views. But there is no extra space because the first one has width match_parent which will make it take the width of the entire parent(no extra space)

Community
  • 1
  • 1
committedandroider
  • 8,711
  • 14
  • 71
  • 126
  • The system calculates the `weight_sum` since you didn't give it one. This comes to 2 so it then divvies up the `width` accordingly which comes to half for both. What exactly are you trying to do (what do you want it to be)? To use `weight` correctly, your width should be "0dp" in a horizontally oriented `LinearLayout` – codeMagic Jun 24 '14 at 21:55
  • it produces the right result but i thought layout_weight just had to do with dividing up extra space and in this case, there was no extra space – committedandroider Jun 24 '14 at 22:03
  • 1
    No, that is incorrect. If the previous views don't use weight then you can use it to give others the remaining space. But giving a weight to each view makes them act accordingly. So, it you had weight of .3 on one and .6 on the other then the latter would take up approx. 2/3 – codeMagic Jun 24 '14 at 22:06
  • thx that makes more sense – committedandroider Jun 24 '14 at 22:12
  • Adding on to that. If you had 3 views(with no extra space) and two of them had weights of 1 assigned to them and one did not specify weight. How would that work in terms of what you just stated? – committedandroider Jun 24 '14 at 22:17
  • What would the width be of the third view? Anyway, you would have to play with it. I'm not an expert on the subject and it depends on various properties – codeMagic Jun 24 '14 at 22:18

1 Answers1

1

How is this happening ?

To fix the ideas, let's say that LinearLayout's width is 100px.

  • TextView Home is MATCH_PARENT so 100px
  • TextView About is MATCH_PARENT so 100px

So, total width used by children is 100px+100px = 200px

Now, let's compute the remaining width : this is the difference between the available width and the width used by all children :

RemainingWidth = AvailableWidth (100px) - TotalWidthUsedByChildren (200px) = -100px

Note that it is negative

There is 2 child views, so distribute the remaining width to each of them according their weight. In this case each child receive 50% of the remaining... it means that each child receive -50px.

So finally :

  • TextView Home width is 100px + -50px = 50px
  • TextView About width is 100px + -50px = 50px

Perfectly logic, but not very intuitive. So the recommendation when using layout_weight is to always set the width of chid views to 0.

ben75
  • 29,217
  • 10
  • 88
  • 134