How to align 3 buttons in a row, Android?

I have a linear vertical layout in the parent. Then the horizontal linear layout at the bottom, including 3 buttons

I want the 1st button on the left side of the activity at the bottom, the 2nd button in the center and the third button on the right side

Here is the xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/viewImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@null" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:layout_gravity="fill_vertical" android:text="Button" /> </LinearLayout> 

+7
android alignment button android-linearlayout
source share
3 answers

For this type of alignment (one button in the far left corner of the parent, another in the center and another in the far right color layout) RelativeLayout will be more convenient than LinearLayout . Because if you use RelativeLayout as the parent layout for your buttons, you can use android:layout_alignParentLeft="true" for the one button that you want to align to the left , android:layout_centerInParent="true" for center alignment and android:layout_alignParentRight="true" to align the button in the side corner to the right .

Try it.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/viewImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@null" android:src="@drawable/ic_launcher" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Button" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="Button" /> </RelativeLayout> </LinearLayout> 
+11
source share

Set the orientation to horizontal

  <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button2" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button3" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:layout_gravity="fill_vertical" android:text="Button" /> </LinearLayout> 

and use the scales for the buttons

Snap

enter image description here

+4
source share

yes, you can do this by applying layout_weight=1 :

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_alignParentBottom="true"> <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="left " /> <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Center" /> <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Right" /> </LinearLayout> </RelativeLayout> 
+2
source share

All Articles