tags:

views:

1239

answers:

2

friends,

i have following three button in linear layout with width fill_parent

now how can i set width of these buttons to cover whole screen area equally?

any help would be appriciated.

<Button 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/btnReplyMessage" 
            android:gravity="left"
            android:text="Reply" />

     <Button 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/btnMarkAsUnread"
            android:gravity="left" 
            android:text="Mark as unread" />

            <ImageButton android:id="@+id/btnDeleteMessage"
                        android:src="@drawable/imgsearch"
                        android:gravity="right" android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:layout_alignParentRight="true"
        />
A: 

You should just specify these attributes for each button:

android:layout_width="fill_parent"
android:layout_weight="1"

So it should be something like that:

<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"/>
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>
Fedor
+2  A: 

Give all buttons the following properties

android:layout_width="fill_parent"
android:layout_weight="1"

fill_parent tells them to consume as much width as possible, and weight determines how that width shall be distributed, when more than one control are competing for the same space. (Try playing around with different values of weight for each button to see how that works)

David Hedlund
it is only applicable for buttons or Edittext too??
UMMA
pretty much everything you can place in a `LinearLayout` will have this behavior. you can play around with setting some values to `wrap_content` and some to `fill_parent`/`weight=1`. in that case the former will consume the space they need, and the latter divide the rest of the available space between them, according to their respective weight. if you set `orientation=vertical` at the parent `LinearLayout`, you can do the same thing with `height` instead.
David Hedlund