Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align two-button layout left and right in Android

Tags:

android

I want to align two buttons with a linear layout, one on the left, and one on the right, like the next and previous buttons on an image gallery. I tried to align them but it doesn't work.

XML layout code:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"     android:background="@android:color/white"     android:gravity="bottom" >       <LinearLayout         android:id="@+id/linearLayout1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:background="@android:color/black" >          <Button             android:id="@+id/button1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="prev"             android:layout_alignParentRight="true" />          <Button             android:id="@+id/button2"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="next" />      </LinearLayout>  </LinearLayout> 

Actual output:

Expected output:

How can I fix it?

like image 695
Dinesh Avatar asked Jun 27 '12 09:06

Dinesh


People also ask

How do you align buttons in relative layout?

The 2 buttons are placed below the TextView having id textView. This is done by using the android:layout_below="@+id/textView" attribute in both the Button tags. We have aligned both the buttons from the top margin (of each other), using android:layout_alignTop="@id/button" attribute.

How to use Relative layouts?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

How do you place a button at the bottom of the screen in Android?

From Settings, go to System, Gestures, and then tap System Navigation. Gesture navigation will be selected by default, but you can tap 3-button navigation to make buttons appear at the bottom of your screen.


2 Answers

Use a RelativeLayout. There you can set android:layout_alignParentLeft and android:layout_alignParentRight. This should work for you:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"     android:background="@android:color/white"     android:gravity="bottom" >       <RelativeLayout         android:id="@+id/relativeLayout1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:background="@android:color/black" >          <Button             android:id="@+id/button1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="prev"             android:layout_alignParentLeft="true" />          <Button             android:id="@+id/button2"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="next"             android:layout_alignParentRight="true"/>      </RelativeLayout>  </LinearLayout> 
like image 172
D-32 Avatar answered Sep 28 '22 00:09

D-32


With Linear Layout

<LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content">          <LinearLayout             android:layout_width="0dp"             android:layout_height="wrap_content"             android:layout_weight="1">              <Button                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="Prev"/>         </LinearLayout>           <Button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="Next"/>  </LinearLayout> 

With Relative Layout

<RelativeLayout         android:layout_width="match_parent"         android:layout_height="wrap_content">          <Button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="Prev"             android:layout_alignParentLeft="true"/>          <Button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="Next"             android:layout_alignParentRight="true"/> </RelativeLayout> 
like image 23
App Kart Avatar answered Sep 28 '22 00:09

App Kart