user3968848
user3968848

Reputation: 119

Android XML: Centring a view in relative layout- android:layout_centerHorizontal="true" not working?

I am trying to center the bottom view ( android:id="@+id/tvStroopCountdown" ) in my XML layout using android:layout_centerHorizontal="true".

However it is not working, instead it is positioned to the left on the screen.

Why not, and how can I solve this?

Full XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/menubackground"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvStroopColor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="3dp"
            android:layout_marginTop="11dp"
             android:layout_marginBottom="15dp"
            android:text="  "
            android:textSize="50dp" 
            android:background="#ffffff"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center" >

        <Button
            android:id="@+id/btnStroop1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" " 
            android:textSize="40dp"
            />

        <Button
            android:id="@+id/btnStroop2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" " 
            android:textSize="40dp"
            />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tvStroopResults"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="3dp"
            android:text="  "
            android:layout_centerHorizontal="true"
            android:textSize="35dp" />

        <TextView
            android:id="@+id/tvStroopScore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvStroopResults"
            android:layout_centerHorizontal="true"
            android:paddingBottom="3dp"
            android:text=" "
            android:textSize="35dp" />

        <TextView
            android:id="@+id/tvStroopSeeMeditation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvStroopScore"
            android:layout_centerHorizontal="true"
            android:paddingBottom="3dp"
            android:text=" "
            android:textSize="35dp" />

        <TextView
            android:id="@+id/tvStroopSeeAttention"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvStroopSeeMeditation"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="4dp"
            android:text=" "
            android:textSize="35dp" />

        <TextView
            android:id="@+id/tvStroopCountdown"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvStroopSeeAttention"
            android:layout_centerHorizontal="true"
            android:ems="10"
            android:textSize="30dp" 
            android:hint=" " >
        </TextView>
    </RelativeLayout>

</LinearLayout>

Upvotes: 4

Views: 4689

Answers (2)

Paul Boddington
Paul Boddington

Reputation: 37645

Change android:layout_centerHorizontal="true" to android:gravity="center_horizontal". What you've written centres the TextView within the RelativeLayout, but displays the text at the left of the TextView. This won't work because your TextView has the same width as the RelativeLayout because you used fill_parent.

Upvotes: 7

Zahan Safallwa
Zahan Safallwa

Reputation: 3914

Use (android:layout_width="wrap_content") instead of "fill_parent". Then everything will be alright. So the XML for that TextView will be like below:

 <TextView
        android:id="@+id/tvStroopCountdown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvStroopSeeAttention"
        android:layout_centerHorizontal="true"
        android:hint="this is right"
        android:textSize="30dp"
       />

Upvotes: 0

Related Questions