Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView will not center its content

Tags:

android

<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?

like image 983
Tom Macdonald Avatar asked Nov 26 '13 10:11

Tom Macdonald


People also ask

How do you center text in TextView?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

How can I center my text in android?

To center align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “center” in layout file, or programmatically set the textAlignment property of the TextView object with View. TEXT_ALIGNMENT_CENTER in activity file.

How do I center a view in XML?

You can apply a centering to any View, including a Layout, by using the XML attribute android:layout_gravity". You probably want to give it the value "center".

How do I center text in a text box in android Studio?

To align text in TextView at the center vertically and horizontally we need to make use of gravity attribute. Example 1 : You can use center_vertical | center_horizontal value to set the text at the center of the TextView area.


2 Answers

Add android:gravity="center" to your textView

like image 121
Manishika Avatar answered Sep 23 '22 02:09

Manishika


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"
        />
like image 42
Damien R. Avatar answered Sep 22 '22 02:09

Damien R.