How do I center text horizontally and vertically in a TextView?

Viewed 1438102

How do I center the text horizontally and vertically in a TextView, so that it appears exactly in the middle of the TextView in Android?

72 Answers

I'm assuming you're using XML layout.

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
    android:text="@string/**yourtextstring**"
/>

You can also use gravity center_vertical or center_horizontal according to your need.

As @stealthcopter commented, in java: .setGravity(Gravity.CENTER);.

And for Kotlin users, .gravity = Gravity.CENTER

android:gravity="center" 

This will do the trick

Use android:textAlignment="center"

       <TextView
        android:text="HOW WAS\nYOUR\nDAY?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:id="@+id/textView5"
         />

TextView gravity works as per your parent layout.

LinearLayout:

If you use LinearLayout then you will find two gravity attribute android:gravity & android:layout_gravity

android:gravity : represent layout potion of internal text of TextView while android:layout_gravity : represent TextView position in parent view.

enter image description here

If you want to set text horizontally & vertically center then use below code this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="300dp"
    android:background="@android:color/background_light"
    android:layout_height="300dp">

    <TextView
        android:layout_width="match_parent"
        android:text="Hello World!"
        android:gravity="center_horizontal"
        android:layout_gravity="center_vertical"
        android:layout_height="wrap_content"
    />
</LinearLayout>

RelativeLayout:

Using RelativeLayout you can use below property 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. android:layout_centerHorizontal="true" if you want TextView in horizontally center of parent view. android:layout_centerVertical="true" if you want TextView in vertically center of parent view.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="300dp"
    android:background="@android:color/background_light"
    android:layout_height="300dp">

    <TextView
        android:layout_width="match_parent"
        android:text="Hello World!"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"
    />
</RelativeLayout>

Actually, we can do better by excluding fontPadding.

<TextView
  android layout_height="wrap_content"
  android layout_height="wrap_content"
  android:includeFontPadding="false"
  android:textAlignment="center"
/>

We can achieve this with these multiple ways:-

XML method 01

<TextView  
    android:id="@+id/textView"
    android:layout_height="match_parent"
    android:layout_width="wrap_content" 
    android:gravity="center_vertical|center_horizontal"
    android:text="@strings/text"
/>

XML method 02

<TextView  
    android:id="@+id/textView"
    android:layout_height="match_parent"
    android:layout_width="wrap_content" 
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@strings/text"
/>

XML method 03

<TextView  
    android:id="@+id/textView"
    android:layout_height="match_parent"
    android:layout_width="wrap_content" 
    android:gravity="center"
    android:text="@strings/text"
/>

XML method 04

<TextView  
    android:id="@+id/textView"
    android:layout_height="match_parent"
    android:layout_width="wrap_content" 
    android:layout_centerInParent="true"
    android:text="@strings/text"
/>

Java method 01

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

Java method 02

textview.setGravity(Gravity.CENTER);

Java method 03

textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
 <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


       <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HELLO"
            android:textColor="@color/colorPrimaryDark"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

 </android.support.constraint.ConstraintLayout>

If you are making your text view width and height to wrap content then you have to manage it's position via layout gravity according to the parent supported attributes. Else you can do.

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"/> 

For kotlin:

If you want to center your TextView from code:

textView.gravity = Gravity.CENTER

If you want to horizontally center:

textView.gravity = Gravity.CENTER_HORIZONTAL

Or, vertically center:

textView.gravity = Gravity.CENTER_VERTICAL
<TextView  
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center"
android:text="In the center"/>

Above is to set it using .xml file.

but I prefer java version because you can also disable it in code while in running condition if necessary. Following is java code.

textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
android:textAlignment="center"

this will do the trick

For the layout of text, it is better to use

android:textAlignment="center"

and for the other layout of objects etc, it's good to use

android:gravity="center"

try this :

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
/>

Let's say Full Textview in screen

 <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello World!"
    android:gravity="center"
    />

Let's say Textview(not full) in Relative layout

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:layout_centerInParent=true
    />

Let's say Textview(not full) in Linear_layout

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:layout_gravity="centre"
    />

You can set the gravity to center vertical and textalignment to center.

<TextView
     android:text="@string/yourText"
     android layout_height="wrap_content"
     android layout_height="wrap_content"
     android:gravity="center_vertical"
     android:textAlignment="center" />
android:gravity="center" 

This line in xml would help you to center the TextView.

You must set the Gravity like this:

(textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

Try this:

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" />

Try this:

Using LinearLayout if you are using textview height and width match_parent You can set the gravity text to center and text alignment to center text horizontal if you are using textview height and width wrap_content then add gravity center attribute in your LinearLayout.

XML Code

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

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textAlignment="center"
    android:gravity="center"
    android:textColor="#000"
    android:textSize="30sp"
    android:text="Welcome to Android" />
    </LinearLayout>

Java code

You can programatically do like this:-

(textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

Thank you

Honorable Mention for the Belated GUI

I wanted to add some value to this already fantastic thread. And finally after 11 years we now have a way to drag and drop the TextView from the toolbox and select gravity through a GUI Properties box. AND, thankfully the GUI combines the properties concisely as "center_vertical|center_horizontal".

enter image description here

    <TextView
    android:text="This is my sample text."
    android:layout_width="match_parent"
    android:layout_height="35.7dp"
    android:id="@+id/textView2"
    android:layout_marginBottom="24.7dp"
    android:typeface="sans"
    android:textSize="20"
    android:gravity="center_vertical|center_horizontal" />

Rearranging the location of the layout, image or textview comes under the topic of alignment. so if you want to put your image src in middle vertically, you should use the code below written

android:layout_centerVertical="true"

and for center Horizontally

android:layout_centerHorizontal="true"

or else, I think we can also use gravity to put every element in mid of parent layout like android:gravity="center"

To show text in the center like:

------------------>text<-------------------

Use these styles:

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="wrap_contect" 
    android:gravity="center"
    android:text="@string/text"
/>

android:gravity="center" 
There are 2 ways to do this :

1. From Xml
     use :  
        <TextView
         .........
        android:gravity="center"/>

2. From Java class
    use : 

     [textview].setGravity(Gravity.CENTER);

You can center a Text View using android:gravity="center"

The code goes as follows

    <TextView
     android:text="@string/yourText"
     android layout_height="wrap_content"
     android layout_height="wrap_content"
     android:gravity="center" />

If that TextView doesn't have the height / width attributes set to wrap_content, just use android:gravity="center". But some people complain that using that does not work. That's because their layout doesn't have the same gravity. You can set

android:layout_gravity="center" IN THE TEXTVIEW (this will apply only to the TextView)

or

android:gravity="center" IN THE PARENT LAYOUT (this will apply to all the views inside it)

use android:gravity="center" to parent

<LinearLayout
    android:layout_width="200dp"
    android:layout_height="200dp"

    android:gravity="center"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Hello world"
        />
</LinearLayout>

Here is solution.

<TextView  
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center"
       android:text="**Your String Value**" />

You can changeandroid:gravity for this.

android:gravity="center" or

if you want to center horizontal or vertical

android:gravity="center_horizontal"

android:gravity="center_vertical"

You can programatically do this in java with: textview.setGravity(Gravity.CENTER);

Textview width and height should be match_parent and add android:gravity="center" in your textview attributes.

If you have only one single TextView into your XML file then you can make the parent view gravity as the centre. This will make your child element in the centre of the screen and then you can make your child view as a wrap for both height and width.

just add this in your xml.

android:gravity="center" 
Related