Sonhja
Sonhja

Reputation: 8448

RelativeLayout centering items not working

I have the following code that has a TextView and a Button. Both of them should look centered:

<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:background="@drawable/register_background"
    android:gravity="center_horizontal" >

<TextView
    android:id="@+id/text_NoProfile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:text="No items in profiles database." />

<Button
    android:id="@+id/buttonAddProfile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/button_style"
    android:text="Add profile"
    android:layout_below="@id/text_NoProfile"
    android:textColor="#ffffff" />
</RelativeLayout>

But when I go to the view, it looks like this:

enter image description here

Why is it not aligned? I thought the gravity="center_horizontal" should do the trick...

Upvotes: 0

Views: 98

Answers (4)

kamran hatami
kamran hatami

Reputation: 122

Add android:layout_centerHorizontal="true" to both elements.

Upvotes: 1

LordCommanDev
LordCommanDev

Reputation: 922

Try with this

<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:layout_centerInParent="true"
android:gravity="center">

<TextView
    android:id="@+id/text_NoProfile"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:text="No items in profiles database."
    android:gravity="center"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="41dp"
    android:layout_below="@+id/text_NoProfile"
    android:layout_centerHorizontal="true" />

Upvotes: 1

eManna
eManna

Reputation: 2482

gravity = "center_horizontal" will not work because the parent of your RelativeLayout is not LinearLayout nor FrameLayout, you should use android:layout_centerHorizontal="true" instead like the following:

<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:background="@drawable/register_background"
    android:layout_centerHorizontal="true" >

<TextView
    android:id="@+id/text_NoProfile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:layout_centerHorizontal="true"
    android:text="No items in profiles database." />

<Button
    android:id="@+id/buttonAddProfile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/button_style"
    android:text="Add profile"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/listView_users"
    android:textColor="#ffffff" />
</RelativeLayout>

Upvotes: 2

Nigam Patro
Nigam Patro

Reputation: 2770

Remember always, in RelativeLayout gravity attribute is not available, that's why its is not working. To make elements center aligned specify layout_centerHorizontal attribute as true.Otherwise take LinearLayout instead of RelativeLayout.

Upvotes: 2

Related Questions