By pupeno


2009-01-11 00:27:55 8 Comments

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?

30 comments

@Chathura Wijesinghe 2019-06-05 01:09:33

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

@SAurabh 2019-06-18 06:55:31

textAlignment="center" won't do the job until the TextView has dimensions to match_parent

@Chathura Wijesinghe 2019-06-19 02:22:04

@SAurabh above example is working without any issues.

@SAurabh 2019-09-20 07:30:30

is it working without gravity to parent or layout gravity in itself? It won't Text alignment will align the text within its view bounds so until the textView takes match_parent in this case it it won't seem like this without gravity in its parent layout or layout_gravity in itself

@Rohit Patil 2019-05-10 19:13:24

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.

@Jocelin 2019-04-27 07:06:31

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

@Rishabh Saxena 2019-04-01 07:12:34

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

@Android Geek 2019-03-26 05:44:45

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

@DEVSHK 2019-03-25 12:02:25

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

@mohamad sheikhi 2019-02-16 05:08:04

Try this:

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

@mohamad sheikhi 2019-02-07 04:24:52

You must set the Gravity like this:

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

@Avijit Karmakar 2015-12-17 06:12:50

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>

@Alpesh Sorathiya 2017-09-20 12:08:17

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>

@Edric 2019-03-27 12:12:55

Btw, any reason you rollbacked to the previous revision? The revision that you reverted is a good practice so that as what the editor has mentioned can help text editors with encoding.

@Attif 2016-03-07 13:42:51

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

@Awesome Robot 2018-12-29 13:35:57

android:gravity="centre" 

This would help you to centre the textview.

@Joshua Best 2018-12-14 16:56:49

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

@hash 2018-11-05 11:12:31

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"

@Muklas 2016-09-04 19:33:07

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

@Bill 2009-01-11 01:53:17

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.

and as @stealthcopter commented in java: .setGravity(Gravity.CENTER);

@Rob 2011-08-17 19:00:29

This doesn't work when used with a RelativeLayout where the layout's height & width are set to wrap_content

@JoJo 2011-11-17 04:06:42

@Rob, if the width and height are wrap_content, then technically, the text is already centered.

@user517491 2012-03-10 21:43:29

If I want to align TextView relative to another view, but it's text centered in itself?

@Eugene van der Merwe 2012-07-23 20:20:28

If you're a minimalist like me you can drop the layout tags.

@uSeRnAmEhAhAhAhAhA 2013-09-20 08:00:09

This isn't working for me (android:gravity="center"). It still shows at far left of screen. :(

@Daniel Jonker 2014-07-09 04:15:34

it sounds like your Textviews width and height are set to wrap_content. If this is the case you can set layout_gravity to center

@Rudi Kershaw 2014-12-22 14:28:56

Not to be confused with android:layout_gravity="center", which does something else.

@bheatcoker 2015-03-19 10:04:25

In a Relative Layout if you set: android:layout_width="wrap_content" android:layout_height="wrap_content" and android:gravity="center" that works for me.

@user3207655 2015-11-03 06:18:47

gravity=center not working. Textview height=34dp and width=100dp.Please help

@Jeel Shah 2016-08-13 16:55:19

Don't forget, there is also center_veritcal and center_horizontal.

@Vasant 2016-08-26 05:05:30

@ Rob,relative layout provides android:layout_centerInParent="true"

@Moses Aprico 2016-09-07 05:51:12

you can also set manually the height and width to certain value to implement gravity.

@jungledev 2017-02-20 17:40:46

I needed to make sure the height was set to match_parent before I could get gravity="center" to work vertically in addition to working horizontally. (because if I left it as wrap-content, even if the content was in the corner of the parent, it didn't 'look' like it was centered, even though it technically was, but the height was less than the parent)

@CoolMind 2018-02-28 08:47:21

The key is android:layout_height="match_parent" and android:gravity="center". If height is "wrap_content", it won't work.

@gareth 2019-11-14 11:39:45

That centers the TextView, the question is how to center the text inside the TextView and I believe the answer is:- android:textAlignment="center"

@ArghadipDasCEO 2018-02-10 16:05:45

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

@OkDroid 2018-06-13 05:50:03

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

@Jatin Mandanka 2018-05-25 08:12:00

Here is solution.

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

@TejpalBh 2018-05-08 09:25:07

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>

@user9680608 2018-04-22 04:41:58

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

@Aravinth Velusamy 2018-03-20 15:10:48

Use Gravity Property in the TextView to center the Text Vertically and Horizontally.

android:gravity="center"

@hyperCoder 2018-03-03 19:33:56

Via .xml file gravity can be set to center as follows

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

Via java following is the way you can solve your particular issue

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

@user8716106 2018-02-05 10:43:31

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

@itzhar 2017-12-14 16:58:03

In ConstraintLayout for any view:

(When you are using a constraint layout, set height & width to 0, and let the constraint to manage view bounds.)

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

@Fenil Patel 2017-12-18 07:06:38

How to center a view or its content e.g. TextView or Button horizontally and vertically? layout_gravity can be used to position a view in the center of its parent. While gravity attribute is used to position view’s content e.g. “text” in the center of the view.

( 1 ) Center TextView Horizontally

enter image description here

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
       android:orientation="vertical"
       android:layout_margin="10dp"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_gravity="center"
        android:background="#333"
        android:textColor="#fff"
        />

</LinearLayout>

( 2 ) Center TextView Vertically

android:layout_gravity=”center_vertical” dose not work!!

To center TextView vertically, you need to make a trick. Place TextView inside a RelativeLayout then set the TextView attribute android:layout_centerInParent=”true” enter image description here

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
       android:orientation="vertical"
       android:layout_margin="10dp"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_centerInParent="true"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:background="#333"
        android:textColor="#fff"
        />

</RelativeLayout>

( 3 ) Center Text Horizontally

android:gravity=”center_horizontal"

enter image description here

( 4 ) Center Text Horizontally & Vertically

android:gravity=”center” this will center the text horizontally and vertically

enter image description here

<TextView
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:text="Hello World!"
    android:gravity="center"
    android:background="#333"
    android:textColor="#fff"
    />

@Thunder 2017-12-03 18:05:48

I have added xml snippet to make a textview center alligned in both Linear and Relative Layout.

In Linear Layout

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text In Center"
android:layout_gravity="center"
android:gravity="center"/>

In Relative Layout

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Text In Center"
android:layout_centerInParent= true/>

@Alejandro Bertinelli 2017-11-27 19:04:44

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)

@Sarvesh Thiruppathi 2017-11-13 10:42:48

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

@Bapusaheb Shinde 2017-09-20 03:34:29

try this :

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

Related Questions

Sponsored Content

18 Answered Questions

[SOLVED] Is it possible to have multiple styles inside a TextView?

33 Answered Questions

[SOLVED] Auto Scale TextView Text to Fit within Bounds

22 Answered Questions

[SOLVED] Can I underline text in an Android layout?

18 Answered Questions

[SOLVED] How do I align views at the bottom of the screen?

33 Answered Questions

[SOLVED] How do I make links in a TextView clickable?

24 Answered Questions

[SOLVED] Android TextView Justify Text

27 Answered Questions

[SOLVED] Making TextView scrollable on Android

  • 2009-11-17 13:43:03
  • Muhammad Maqsoodur Rehman
  • 542991 View
  • 731 Score
  • 27 Answer
  • Tags:   android scroll textview

8 Answered Questions

[SOLVED] How to click or tap on a TextView text

3 Answered Questions

3 Answered Questions

Sponsored Content