Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align two buttons horizontally [duplicate]

I have two buttons and I want to put them horizontally so the screen they will be beside each other, there is not space left, I know I can do it with linearlayout and set weight but I want relativelayout because I already have weight in my XML so it will be bad performance to put two weights in each other.

I tried like this

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_selector" >

    <Button
        android:id="@+id/b_orderMeal_send"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="4dip"
        android:background="@drawable/button_selector"
        android:clickable="true"
        android:focusable="true"
        android:text="@string/b_send"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/b_orderMeal_cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="4dip"
        android:background="@drawable/button_selector"
        android:clickable="true"
        android:focusable="true"
        android:text="@string/b_cancel"
        android:textColor="#FFFFFF" />
</RelativeLayout>

But just the cancel button is appeard and that is because I used "fill parent", what is the solution please?

like image 517
Marco Dinatsoli Avatar asked Feb 18 '13 13:02

Marco Dinatsoli


People also ask

How do I align two buttons on the same line?

If you have multiple buttons that should sit side-by-side on the same line, add the data-inline="true" attribute to each button. This will style the buttons to be the width of their content and float the buttons so they sit on the same line.

How do you align buttons horizontally?

We can align the buttons horizontally as well as vertically. We can center the button by using the following methods: text-align: center - By setting the value of text-align property of parent div tag to the center. margin: auto - By setting the value of margin property to auto.

How do you center two buttons next to each other?

Sometimes you might want to have two buttons next to each other, but to center both together on the page. You can achieve this by wrapping both buttons in a parent <div> and using flexbox to center them on the page. Notice that we also added margin-right: 20px to the first button, in order to add space between them.

How do I align two vertical buttons in HTML?

Add vertical-align: top; to list items <li> s. That was it!


1 Answers

Try this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:layout_weight="1"
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button" />
     <Button
           android:layout_weight="1"
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
like image 144
Pragnani Avatar answered Oct 24 '22 10:10

Pragnani