Pablo Fernandez
Pablo Fernandez

Reputation: 287750

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

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?

Upvotes: 2335

Views: 1550630

Answers (30)

Bruce
Bruce

Reputation: 3548

Just leaving this here in case it saves someone the 2 hours it took me to find :

If your text is too big in a given dimension, it won't attempt to center it in that dimension. That makes sense, but a TextView has 'font padding' by default. So it's possible to set a text size such that the height looks like it fits comfortably inside your text view, but in fact it is 'too big' because it encroaches on the font padding. In this situation, the text will NOT be vertically centered, the request will be ignored. The solution is simple - just remove this annoying font padding with:

android:includeFontPadding="false"

Then it centers vertically as expected.

Upvotes: 0

Sep
Sep

Reputation: 371

as mentioned in other answers, you can set gravity in xml with:

android:gravity="center"

and in java code :

textview.setGravity(Gravity.CENTER)

in kotlin code :

textview.gravity = Gravity.CENTER

but if you are doing this in kotlin compose, you can center gravity using :

textAlign = TextAlign.Center

Upvotes: 5

Hamid Raza Goraya
Hamid Raza Goraya

Reputation: 159

Font padding can also affect the position of text you can remove it by adding includefontpadding=false

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

Upvotes: 1

TARUN SAINI
TARUN SAINI

Reputation: 61

just add this in your xml.

android:gravity="center" 

Upvotes: 4

Ashish Kumar
Ashish Kumar

Reputation: 1126

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" 

Upvotes: 2

Haechan
Haechan

Reputation: 63

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"

Upvotes: 3

Sarvesh Thiruppathi
Sarvesh Thiruppathi

Reputation: 275

android:gravity="center" 

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

Upvotes: 1

Bill
Bill

Reputation: 34802

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

Upvotes: 3435

Raushan Kumar
Raushan Kumar

Reputation: 127

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"

Upvotes: 1

Leslin Antony
Leslin Antony

Reputation: 21

android:textAlignment="center"

this will do the trick

Upvotes: 2

Cunity
Cunity

Reputation: 181

Try this way,it will work

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">
  
        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"/>

</LinearLayout>

Upvotes: 10

Sting
Sting

Reputation: 342

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" />

Upvotes: 1

Jamil Hasnine Tamim
Jamil Hasnine Tamim

Reputation: 4448

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

Upvotes: 3

Seanghay
Seanghay

Reputation: 1086

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"
/>

Upvotes: 9

Josss
Josss

Reputation: 259

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

Upvotes: 0

Chathura Wijesinghe
Chathura Wijesinghe

Reputation: 3349

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"
         />

Upvotes: 12

Rohit Patil
Rohit Patil

Reputation: 1625

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.

Upvotes: 0

Rishabh Saxena
Rishabh Saxena

Reputation: 1795

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"/> 

Upvotes: 3

Android Geek
Android Geek

Reputation: 9225

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

Upvotes: 1

DEVSHK
DEVSHK

Reputation: 863

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

Upvotes: 3

mohamad sheikhi
mohamad sheikhi

Reputation: 394

Try this:

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

Upvotes: 1

mohamad sheikhi
mohamad sheikhi

Reputation: 394

You must set the Gravity like this:

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

Upvotes: 1

Avijit Karmakar
Avijit Karmakar

Reputation: 9408

If the TextView's height and width are wrap content then the text within the TextView always be centered. But if the TextView's width is match_parent and height is match_parent or wrap_content then you have to write the below code:

For RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</RelativeLayout>

For LinearLayout:

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

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

</LinearLayout>

Upvotes: 20

Alpesh Sorathiya
Alpesh Sorathiya

Reputation: 508

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>

Upvotes: 11

Attif
Attif

Reputation: 1150

The TextView's height and width are wrap content then the text within the textview always be centered, then make center in its parent layout by using:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello.."/>
</RelativeLayout>

For LinearLayout also the code is same :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello.."/>
</LinearLayout>

and pro-grammatically parent is RelativeLayout java code this at run time use something like this in your activity

TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);

Upvotes: 9

Joshua Best
Joshua Best

Reputation: 246

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

Upvotes: 0

hash
hash

Reputation: 5416

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"

Upvotes: 0

Muklas
Muklas

Reputation: 688

center text

<LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_height="wrap_content">
    <TextView
            android:id="@+id/failresults"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:gravity="center_vertical|center_horizontal"
            android:text=""
            android:textSize="12sp" />
</LinearLayout>

if you have textview inside linearlayout, you need set them both gravity to center

Upvotes: 5

ArghadipDasCEO
ArghadipDasCEO

Reputation: 187

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);

Upvotes: 8

OkDroid
OkDroid

Reputation: 195

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" />

Upvotes: 1

Related Questions