Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center two buttons horizontally

Tags:

android

layout

I try to arrange two buttons (with images on them which work fine) next to each other and to center them horizontally. That's what I have so far:

<LinearLayout android:orientation="horizontal" android:layout_below="@id/radioGroup"               android:layout_width="wrap_content" android:layout_height="wrap_content"               android:layout_gravity="center_horizontal|center">     <Button             android:id="@+id/allow"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_below="@id/radioGroup"             android:layout_gravity="center_horizontal"             android:drawableLeft="@drawable/accept_btn"             android:text="Allow"/>     <Button             android:id="@+id/deny"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_toRightOf="@id/allow"             android:layout_below="@id/radioGroup"             android:layout_gravity="center_horizontal"             android:drawableLeft="@drawable/block_btn"             android:text="Deny"/> </LinearLayout> 

Unfortunately they are still aligned to the left side. Any help is appreciated! Yves

Edit:

Unfortunately none of the comments or suggestions work so far. That's why I try to provide a simplified, full layout now with a RelativeLayout:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"             android:layout_width="fill_parent" android:layout_height="wrap_content"             android:layout_centerHorizontal="true">     <TextView android:text="@+id/TextView01" android:id="@+id/TextView01"           android:layout_width="wrap_content" android:layout_height="wrap_content"/>     <Button         android:id="@+id/allow"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@id/TextView01"         android:text="Allow"/>     <Button         android:id="@+id/deny"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_toRightOf="@id/allow"         android:layout_alignTop="@id/allow"         android:text="Deny"/> </RelativeLayout> 

I've tried all combinations of attributes in the LinearLayout and on the Button elements without luck. Any other ideas?

like image 556
Yves Avatar asked Nov 15 '10 23:11

Yves


People also ask

How do I center two buttons?

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 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 I align a button horizontally in HTML?

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 put a space between two buttons horizontally on android?

Notice the line that says android:layout_marginTop="10dip" which ensures that you leave a reasonable 10 dip space in between your buttons. Ofcourse, you can increase (or decrease) that space in between your buttons. That's your choice. Hope this answered your question satisfactorily.


1 Answers

This is my solution:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent" android:layout_height="wrap_content"     android:layout_centerHorizontal="true">      <TextView         android:text="@+id/SomeText"         android:id="@+id/TextView01"         android:layout_width="wrap_content" android:layout_height="wrap_content" />      <LinearLayout         android:orientation="horizontal"         android:background="@android:drawable/bottom_bar"         android:paddingLeft="4.0dip"         android:paddingTop="5.0dip"         android:paddingRight="4.0dip"         android:paddingBottom="1.0dip"         android:layout_width="fill_parent" android:layout_height="wrap_content"         android:layout_below="@+id/TextView01">          <Button             android:id="@+id/allow"             android:layout_width="0.0dip" android:layout_height="fill_parent"             android:text="Allow"             android:layout_weight="1.0" />          <Button             android:id="@+id/deny"             android:layout_width="0.0dip" android:layout_height="fill_parent"             android:text="Deny"             android:layout_weight="1.0" />      </LinearLayout> </RelativeLayout> 
like image 188
printminion Avatar answered Sep 30 '22 17:09

printminion