views:

2365

answers:

5

I always read about this funny weight value in the Android documentations. Now I want to try it for the first time but it isn't working at all.

As I understand it from the documentations this layout:

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

     <Button
        android:text="Register"
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        weight="1" />

     <Button
        android:text="Not this time"
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        weight="1" />

  </LinearLayout>

should create two buttons that are horizontally aligned and share the space equally. The problem is the two buttons don't grow to fill the space. The result is something like this:

alt text

I would like the buttons to grow and fill the whole line. If both buttons are set to fill parent only the first button is shown and fills the whole line.

A: 

Perhaps setting both of the buttons layout_width properties to "fill_parent" will do the trick.

I just tested this code and it works in the emulator:

<LinearLayout android:layout_width="fill_parent"
          android:layout_height="wrap_content">

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="hello world"/>

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="goodbye world"/>

</LinearLayout>

Be sure to set layout_width to "fill_parent" on both buttons.

jeremynealbrown
this only pushes the right button out of the screen and shows only the first button.
Janusz
A: 

Try setting the layout_width of both buttons to "0dip" and the weight of both buttons to 0.5

jqpubliq
Now both buttons are vanished from screen
Janusz
okay, then set the layout width on both to fill_parent and weights to 0.5
jqpubliq
again only shows the first button
Janusz
jqpubliq
+1  A: 

I think I found the problem. You are not setting the layout_weight property. Your code reads weight="1" and it should read android:layout_weight="1".

jeremynealbrown
Your are right it seems that does the trick. The are not the same size though the not this time button is bigger I think becaue it has the longer text in it.
Janusz
A: 

Substitute wrap_content by fill_parent.

Humberto Pinheiro
A: 

In the above XML, set the android:layout_weight of the linear layout as 2: android:layout_weight="2"

Priyanka