2 buttons side by side

AndroidUser InterfaceLayout

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

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

Solution 4 - Android

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"

Solution 5 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlucaView Question on Stackoverflow
Solution 1 - Android2red13View Answer on Stackoverflow
Solution 2 - AndroidiarroyoView Answer on Stackoverflow
Solution 3 - AndroidD.SnapView Answer on Stackoverflow
Solution 4 - AndroidDerrickView Answer on Stackoverflow
Solution 5 - AndroidHussainView Answer on Stackoverflow