Making Equal Width Buttons in Android


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".

  1. #1 by Bantam Bytes on November 22, 2010 - 8:02 pm

    First result from Google and just what I was looking for. It works. Thanks.

  2. #2 by Kyle on July 22, 2011 - 2:07 am

    Saved me so much time! Thanks.

  3. #3 by Paul on October 5, 2011 - 11:02 pm

    Thanks for the post. I also wrote an EqualSpaceLayout that is similar to the LinearLayout, but it will not distort the sizes of its child views. This is useful when you have custom image backgrounds. Check it out: http://www.dev-smart.com/archives/225

  4. #4 by Kiran on October 6, 2011 - 4:03 am

    Thanks a lot. Helped.

  5. #5 by sandy on December 18, 2011 - 8:18 am

    thank you

  6. #6 by sachin on March 6, 2012 - 1:28 am

    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

  7. #7 by Niels on February 10, 2013 - 2:55 pm

    Thanks a lot! hours of work finding this.

  8. #8 by adarsh on April 21, 2016 - 1:43 am

    it was ood thanks alot

  9. #9 by adarsh on April 21, 2016 - 1:44 am

    adarsh :
    it was good thanks a lot

(will not be published)

*