I'm trying to center a button in relative layout, is this possible? I've tried the Gravity and Orientation functions but they don't do anything.
+1
A:
Try
android:layout_centerHorizontal="true"
Exactly like this, it works for me:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000"
>
<Button
android:id="@+id/btn_mybutton"
android:layout_height="wrap_content"
android:layout_width="124dip"
android:layout_marginTop="5dip"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
ShadowGod
2010-09-20 02:25:33
This didn't work.
Arcadia
2010-09-20 02:46:22
A:
Arcadia, just try the code below. There are several ways to get the result you're looking for, this is one of the easier ways.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<Button
android:id="@+id/the_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered Button"
/>
</RelativeLayout>
Setting the gravity of the RelativeLayout itself will affect all of the objects placed inside of it. In this case, it's just the button. You can use any of the gravity settings here, of course (e.g. center_horizontal, top, right, and so on).
You could also use this code below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/the_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered Button"
android:layout_centerInParent="true"
/>
</RelativeLayout>
kcoppock
2010-09-20 03:14:20
Thanks. However, if I had multiple buttons and only wanted one centered how would I do that? The first code would center everything within the parent relativelayout. Also, how could I center the button at the top of it's row?
Arcadia
2010-09-20 03:26:59
Just use the second example, and only add centerInParent to the button that you want centered. You can also use centerHorizontal, and layout_alignParentTop to align it to the top and center it horizontally. What exactly are you trying to achieve, I might can give a better example.
kcoppock
2010-09-20 04:48:36
I'm barely beginning my studies of android development and trying to fully understand how to manipulate things to do what I want. Basically I want to stay away from aligning things with pixels because the picture could look very different on a different screen type/resolution. However, when you use the gravity command, everything follows it. I tried disabling gravity for a button by using ignoreGravity and it didnt work. This stuff is very tricky.
Arcadia
2010-09-20 05:01:06
Yeah, it definitely can be. There are plenty of ways to do any particular layout. Best way is to sort of sketch it out in your head how you want it, and go from there. This quick tutorial may help some too: http://developer.android.com/resources/tutorials/views/hello-relativelayout.html
kcoppock
2010-09-20 06:13:52
Yeah I was actually doing that tutorial. I'm going through each of the tutorials and learning how to fully manipulate them, otherwise I won't have a grasp of how each one is different.
Arcadia
2010-09-20 14:31:21