5

i am trying to make a Linear Layout with 5 buttons horizontally placed with equal spacing, but all the buttons size (width) should be 40dp only.

i tried this :

<LinearLayout   android:id="@+id/button_layout"
                    android:background="#DCE1DC"
                    android:orientation="horizontal"
                    android:weightSum="5"
                    android:layout_width="fill_parent"
                    android:layout_height="70dip">

        <Button     android:id="@+id/button_A"
                    android:layout_weight="1"
                    android:layout_height="60dp"
                    android:layout_width="0dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="30dp"/>

        <Button     android:id="@+id/button_B"
                    android:layout_weight="1"
                    android:layout_height="60dp"
                    android:layout_width="0dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="30dp"/>

        <Button     android:id="@+id/button_C"
                    android:layout_weight="1"
                    android:layout_height="60dp"
                    android:layout_width="0dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="30dp"/>

        <Button     android:id="@+id/button_D"
                    android:layout_weight="1"
                    android:layout_height="60dp"
                    android:layout_width="0dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="30dp"/>

        <Button     android:id="@+id/button_E"
                    android:layout_weight="1"
                    android:layout_height="60dp"
                    android:layout_width="0dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="30dp"
                    android:layout_marginLeft="30dp"/>

    </LinearLayout>

its working but i need the buttons width to be smaller , how to acheive this?

Goofy
  • 6,098
  • 17
  • 90
  • 156
  • possible duplicate of [Android: How to make all elements inside LinearLayout same size?](http://stackoverflow.com/questions/1177020/android-how-to-make-all-elements-inside-linearlayout-same-size) – Madan Sapkota Jun 05 '15 at 07:03

2 Answers2

3

i guess this will resolve your query...

as you say you are using Linearlayout then you can do something like...

<LinearLayout
android:layout_width="match_parent"
android:layout_height="yourheight"
android:orientation="horizontal">
<Button 
android:id="@+id/button_name"
android:weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent" />

<Button 
android:id="@+id/button_name"
android:weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent" />

//add as many buttons as you want 


<Button 
android:id="@+id/button_name"
android:weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
</LinearLayout> 
karan
  • 8,637
  • 3
  • 41
  • 78
  • if its a tablet then the width of the button will increase? – Goofy Apr 01 '13 at 19:25
  • it works well for all phones...but with tablets i think it should...still not implemented on one...give it a try...and tell if it workd – karan Apr 01 '13 at 19:28
  • and here you have mention the height of linear layout, i want the button width to be smaller – Goofy Apr 01 '13 at 19:29
  • its just for keeping the height in bound...width will be adjusted automatically – karan Apr 01 '13 at 19:30
  • and if i want the widht to be smaller then? – Goofy Apr 01 '13 at 19:32
  • I am not sure, but you might have to reduce the width of the linearlayout and it will carry on...i have not done anything like that as my code demand was not that much...give it a try once... – karan Apr 01 '13 at 19:37
  • and you can upvote or accept the answer if you find this useful...as it can also help others searching for similar solution – karan Apr 01 '13 at 19:39
  • No such attribute as "android:weight" - did you mean "android:layout_weight"? – loxdog Nov 04 '14 at 11:31
  • you mean android:layout_weight="1" – Martin Sep 13 '18 at 03:42
1

For each button, change this:

android:layout_width="0dp"

To this:

android:layout_width="40dp"
David Manpearl
  • 12,362
  • 8
  • 55
  • 72
  • yes it works but the width changing from device to device, i mean to say if its a tablet then the width if the button is increasing – Goofy Apr 01 '13 at 19:14
  • Yes, this is to be expected. Android changes the width based on device screen density. This is because of "dp" = (Density Independent Pixel, aka "dip"). If you change to "px", then the size will stay the same number of pixels, but be smaller in size on the tablet. You can specify full width "match_parent" and then your two columns will even out to fill the screen evenly because of the "weight". – David Manpearl Apr 01 '13 at 21:50