Tom Macdonald
Tom Macdonald

Reputation: 6583

TextView will not center its content

<TextView
        android:layout_height="30dp"
        android:text="@string/list_title"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:textSize="@dimen/title_size"
        android:textAlignment="center"
        android:layout_alignParentTop="true"
        android:background="@color/white"
        android:id="@+id/list_title"
        />

Is the first element in a RelativeLayout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center">

But the text in the TextView will not center. I cannot understand why as all gravities and the textAlignment property are set to "center".

What am I doing wrong?

Upvotes: 21

Views: 11021

Answers (3)

Amit Bhatiwal
Amit Bhatiwal

Reputation: 71

if your textview is inside a constraintLayout then set its width to 0dp and then apply these constraints

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:textAlignment="center"

Upvotes: 0

Damien R.
Damien R.

Reputation: 3373

Try this:

<TextView
        android:layout_height="30dp"
        android:text="@string/list_title"
        android:gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:textSize="@dimen/title_size"
        android:textAlignment="center"
        android:layout_alignParentTop="true"
        android:background="@color/white"
        android:id="@+id/list_title"
        />

Upvotes: 3

Manishika
Manishika

Reputation: 5564

Add android:gravity="center" to your textView

Upvotes: 67

Related Questions