Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Put two buttons side by side in RelativeLayout

I have a RelativeLayout with two buttons at bottom which are side by side. My goal is to have those buttons side by side but filling the screen width. Can somebody tell me how to do it?

My layout file is:

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Left Button"
    android:id="@+id/button"
    android:layout_alignParentTop="false"
    android:layout_weight="1"
    android:layout_marginTop="77dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="false"
    android:layout_alignParentStart="false" />

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Right Button"
    android:id="@+id/button2"
    android:layout_weight="1"
    android:layout_toRightOf="@id/button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="false"
    android:layout_alignParentStart="false" />
</RelativeLayout>
like image 895
Vicent Avatar asked Jul 29 '15 09:07

Vicent


2 Answers

Put your Buttons in a LinearLayout which has horizontal orientation. And assign weight to your buttons.

Here's your code but modified.

<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"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            tools:context=".MainActivity">\

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="false"
        android:layout_weight="0.5"
        android:text="Left Button"
        />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="Right Button"
        />
</LinearLayout>

like image 57
Noman Rafique Avatar answered Oct 07 '22 23:10

Noman Rafique


<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
    android:id="@+id/dummyView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/dummyView"
    android:text="Left Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/dummyView"
    android:text="Right Button" />

like image 40
Anitha Manikandan Avatar answered Oct 07 '22 21:10

Anitha Manikandan