stavros.3p
stavros.3p

Reputation: 2324

TextView won't center in parent RelativeLayout

I am trying to center a TextView inside a RelativeLayout, but for some reason the position of the TextView is not exactly in the center, it's in the center and a little down. This problem occurs when I add the second RelativeLayout, the one with no id.

Here are 2 ways I tried to solve my problem without success:

First:

<RelativeLayout
   android:id="@+id/minus_one"
   android:layout_width="45dp"
   android:layout_height="match_parent"
   android:background="@color/lightgrey">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/ractangle_visual_feedback_selector"
                android:gravity="center">


            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="22dp"
                    android:textStyle="bold"
                    android:text="-"/>

                </RelativeLayout>
            </RelativeLayout>

Second:

<RelativeLayout
                    android:id="@+id/minus_one"
                    android:layout_width="45dp"
                    android:layout_height="match_parent"
                    android:background="@color/lightgrey">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/ractangle_visual_feedback_selector">


                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_centerHorizontal="true"
                        android:textSize="22dp"
                        android:textStyle="bold"
                        android:text="-"/>

                    </RelativeLayout>
                </RelativeLayout>

Upvotes: 1

Views: 1499

Answers (5)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

#. If your TextView height-width is used as fixed size or wrap_content, then use attribute android:layout_centerInParent="true" to TextView to align it center position of its parent.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light">

    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_green_light"
        android:layout_centerInParent="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="22dp"
            android:textStyle="bold"
            android:text="-"
            android:background="@android:color/white"/>

    </RelativeLayout>
</RelativeLayout>

OUTPUT:

enter image description here

#. If your TextView height-width is used as match_parent, then use attribute android:gravity="center" to TextView to align it center position of its parent.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light">

    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_green_light"
        android:layout_centerInParent="true">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="22dp"
            android:textStyle="bold"
            android:text="-"
            android:background="@android:color/white"/>

    </RelativeLayout>
</RelativeLayout>

OUTPUT:

enter image description here

Hope this will help~

Upvotes: 3

Vidhi Dave
Vidhi Dave

Reputation: 5684

Try below code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/minus_one"
    android:layout_width="45dp"
    android:layout_height="match_parent"
    android:background="@color/gray"
    android:layout_gravity="center">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="22dp"
            android:textAlignment="center"
            android:gravity="center_vertical"
            android:textStyle="bold"
            android:text="-"/>

    </RelativeLayout>
</RelativeLayout>

Upvotes: 1

Muhammed G&#220;NEŞ
Muhammed G&#220;NEŞ

Reputation: 302

try this android:layout_centerInParent="true"

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="22dp"
                android:textStyle="bold"
                android:layout_centerInParent="true"
                android:text="-"/>
        </RelativeLayout>

Upvotes: 2

Jay Rathod
Jay Rathod

Reputation: 11245

Change root Relative Layout android:layout_width to "match_parent". Remove android:gravity="center" from parent Relative Layout no needful. Now add the android:layout_centerInParent="true" to your Text View.

Try this xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/minus_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Testing"
            android:textSize="22dp"
            android:textStyle="bold" />

    </RelativeLayout>
</RelativeLayout>

Upvotes: 5

Jaydeep Khambhayta
Jaydeep Khambhayta

Reputation: 5279

change the layout_width to match parent

 android:layout_width="match_parent"

Upvotes: 2

Related Questions