194

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.

8
  • 1
    Language? Framework? OS? Commented Sep 20, 2010 at 2:00
  • 3
    @Arnold Spence: you beat me by 24 seconds :) Commented Sep 20, 2010 at 2:01
  • 3
    Sorry, this is my first time using Stack Overflow. Yes I'm developing for Android. =)
    – Arcadia
    Commented Sep 20, 2010 at 2:14
  • 7
    No prob. The right tags will get the attention of the right poeple faster. Fixed :) Commented Sep 20, 2010 at 2:21
  • 8
    +1 to Arnold Spence for being considerate. Some people on SO would have verbally destroyed the OP for just being "new".
    – Subby
    Commented Jan 23, 2014 at 11:27

11 Answers 11

412

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>
2
  • 5
    I doesn't work if you have match_parent for your width.
    – Ghoti
    Commented Mar 14, 2014 at 11:28
  • 2
    If you have match_parent for your width, it's already centred horizontally.
    – ataulm
    Commented Sep 4, 2015 at 11:27
116

You can use CENTER_IN_PARENT for relative layout.

Add android:layout_centerInParent="true" to element which you want to center in the RelativeLayout

1
  • 1
    That centers horizontally and veritcally, how 'bout layout_centerHorizontal=true Commented Jul 1, 2020 at 17:15
38

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>
5
  • 1
    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
    Commented Sep 20, 2010 at 3:26
  • 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. Commented Sep 20, 2010 at 4:48
  • 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
    Commented Sep 20, 2010 at 5:01
  • 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: developer.android.com/resources/tutorials/views/… Commented Sep 20, 2010 at 6:13
  • 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
    Commented Sep 20, 2010 at 14:31
29

Summarized

Adding:

android:layout_centerInParent="true"

just works on RelativeLayout, if one of the following attributes is also set for the view:

android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"

which alignes the child view to the parent view. The "center" is based on the axis of the alignement you have chosen:

left/right -> vertical

top/bottom -> horizontal

Setting the gravity of childs/content inside the view:

android:gravity="center"

is centering the child inside the parent view in any case, if there is no alignment set. Optional you can choose:

<!-- Axis to Center -->
android:gravity="center_horizontal"
android:gravity="center_vertical"

<!-- Alignment to set-->
android:gravity="top"
android:gravity="bottom"
android:gravity="left"
android:gravity="right"
android:gravity="fill"
...

Then there is:

android:layout_gravity="center"

which is centering the view itself inside it's parent

And at last you can add the following attribute to the parents view:

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
10

Its easy, don't align it to anything

<Button
    android:id="@+id/the_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true"
    android:text="Centered Button"/>
0
8

Use the attribute android:centerInParent="true" inside a view , when you want to center the view in Relative layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NWE"
        android:textSize="30dp"/>
</RelativeLayout>
0
2

To center align in one of the direction use android:layout_centerHorizontal="true" or android:layout_centerVertical="true" in the child layout

2

It's really easy. Try the code below,

<RelativeLayout
    android:id="@+id/second_RL"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/first_RL"
    android:background="@android:color/holo_blue_bright"
    android:gravity="center">

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

</RelativeLayout>
2
  • 2
    Code only answers are not welcomed here. Try to post more details as to why the above answer will work. :)
    – Vivz
    Commented Aug 7, 2017 at 6:18
  • Thanks for the guideline. I'm new in here.
    – Primesh
    Commented Aug 7, 2017 at 6:21
2

It's pretty simple, just use Android:CenterInParent="true" to center both in horizontal and vertical, or use Android:Horizontal="true" to center in horizontal and Android:Vertical="true"

And make sure you write all this in RelaytiveLayout

1

Removing any alignment like android:layout_alignParentStart="true" and adding centerInParent worked for me. If the "align" stays in, the centerInParent doesn't work

0

you must have aligned it to left or right of parent view

don't use any of these when using android:centerInParent="true" codes:-

android:layout_alignParentLeft="true"

android:layout_alignParentLeft="true"

android:layout_alignRight=""

etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.