62

How can I put 2 buttons side by side, so that they occupy all the width, with a little space between them?

I thought a horiz linear layout, with 2 sub linear layouts set to match parent and weight 1, each of them containing the button. Is there a simpler way? can this be accomplished with relative layouts?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
luca
  • 12,311
  • 15
  • 70
  • 103
  • 1
    possible duplicate of [XML Table layout? Two EQUAL-width rows filled with equally width buttons??](http://stackoverflow.com/questions/2865497/xml-table-layout-two-equal-width-rows-filled-with-equally-width-buttons) – Robby Pond Apr 05 '11 at 12:06
  • dear no need to give orientation just give the padding to button and orientation is by default is horizontal which you require in your application...try it hope it work.. – RobinHood Apr 05 '11 at 13:07
  • you can try tablelayout,two buttons be put into tabrow,i think this meet you – pengwang Apr 05 '11 at 11:57
  • This can be more complex what the question suggest.. since you have to add table layout then after table row then add two buttons. – Jana Apr 05 '11 at 12:03
  • if the problem is not solved check this link http://stackoverflow.com/questions/14892365/linear-layout-with-two-buttons-side-by-side-android – Pranoy Sarkar Aug 21 '16 at 09:53

5 Answers5

125
<LinearLayout 
    android:id="@+id/LinearLayout02" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:layout_alignParentBottom="true">
    <Button 
        android:id="@+id/Button02" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" android:text="Apply">
    </Button>
    <Button 
        android:id="@+id/Button03" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:text="Cancel">
    </Button>
</LinearLayout>
kamiha
  • 666
  • 1
  • 12
  • 29
2red13
  • 11,197
  • 8
  • 40
  • 52
  • That's the first thing I tried, but I got a compiler error trying to apply the layout_weight on buttons.. maybe something else was wrong.. I'll try that again – luca Apr 05 '11 at 14:57
  • that surprises me, i use this code for 1,6 to 2.3 without any problems – 2red13 Apr 05 '11 at 15:01
  • 2
    layout_alignParentBottom (line 5) is invalid for LinearLayout. – SMBiggs Aug 28 '12 at 14:27
  • you're right, but in my Code is the LL a Child of a RelativeLayout, then th LL is shown at the Bottom of the RL – 2red13 Sep 03 '12 at 10:39
  • 3
    This doesn't work. The buttons in this example are of similar width only because the text inside is of similar length. Try using text of radically different length (I'm using "Downloaded" and "All") and have a look at the effect. – pstobiecki Jan 17 '16 at 16:18
5

If you want that the 2 buttons ocuppy all the width and the buttons have the same width, you must change in the 2 buttons the propertie:

android:layout_width="wrap_content" to android:layout_width="match_parent"

because if you have one of this button with a long text and the other button with short text, the button with long text ocuppy more space.

iarroyo
  • 2,354
  • 24
  • 23
5
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:text="button1"
    android:id="@+id/button" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:text="button2"
    android:id="@+id/button2" />

</LinearLayout>
D.Snap
  • 1,704
  • 1
  • 22
  • 15
0

Using LinearLayout, for each button: make the width match_parent, and the weight="1". The margin will provide a little space in between each button. With this, you can add as many buttons in a row as you want, all with similar width.

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp"
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Derrick
  • 75
  • 7
-3

Try this,

Make a RelaiveLayout orientation as horizontal and the give some padding to have a space between them..

Make the Layoutheight and Layoutwidth of ur wish

Thanks

Vidyadhar
  • 1,088
  • 9
  • 15
Hussain
  • 5,552
  • 4
  • 40
  • 50
  • 3
    Orientation is a property of LinearLayout, not RelativeLayout. Even though I don't think it throws a compiler error, it doesn't actually have any effect. – Kevin Grant Jan 06 '14 at 16:04