September 27, 2010 by

It is a common requirement to layout a bunch of buttons side by side. Making them of equal width is just plain good GUI design.

image

To achieve the effect above, first create a LinearLayout just for the buttons. The layout will contain three buttons.


<LinearLayout android_id="@+id/LinearLayout02" android_layout_height="wrap_content" 
  android_layout_width="fill_parent">
  <Button android_layout_height="wrap_content" android_text="Update" 
    android_layout_width="fill_parent" android_layout_weight="1"/>
  <Button android_layout_height="wrap_content" android_text="New" 
    android_layout_width="fill_parent" android_layout_weight="1"/>
  <Button android_layout_height="wrap_content" android_text="Delete" 
    android_layout_width="fill_parent" android_layout_weight="1"/>
</LinearLayout>

We took these steps to get the effect:

1. For the LinearLayout we set android_layout_width="fill_parent". This causes the layout view to take up the full width available from the device.

2. For each Button, we set android_layout_width="fill_parent" and android_layout_weight="1".

9 Comments

sachin

Not the perfect ans as the buttons are still of different widths

instead use android:width=”_(your desired size)_dp”
but use linear layout, else it will not work

this is the working code

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *