Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 334
user3968848 Avatar asked Mar 19 '23 15:03

user3968848


1 Answers

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.

like image 67
Paul Boddington Avatar answered Apr 25 '23 11:04

Paul Boddington