Categories
android textview

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

2255

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?

2

  • 13

    set both layout_width and layout_height to fill_parent, then set gravity to center. That’ll do the trick

    – Amila

    Jun 25, 2015 at 9:39

  • 5

    fill_parent is deprecated now so use MATCH_PARENT in layout_width and layout_height and set gravity of the TextView to center.

    Jun 30, 2018 at 9:58

3353

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

15

  • 66

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

    – Rob

    Aug 17, 2011 at 19:00

  • 108

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

    – JoJo

    Nov 17, 2011 at 4:06

  • 4

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

    – user517491

    Mar 10, 2012 at 21:43

  • 10

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

    Sep 20, 2013 at 8:00

  • 40

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

    Dec 22, 2014 at 14:28

476

android:gravity="center" 

This will do the trick

6

  • 35

    The textview has to be match_parent or fill_parent for this to work. If its wrap content, then it will not center.

    Aug 18, 2014 at 15:59

  • 9

    @KalelWade: centering text in a textview with wrap_content makes no sense at all. Maybe you’re thinking of centering a textview in a parent view, in which case, you simiply put this line in the parent.

    Feb 26, 2016 at 20:42

  • 1

    @Mooing Duck Thus the reason to not use wrap content – it doesn’t make sense. But when you’re either coping and pasting or just rushing, you may not put two and two together. That’s why I mentioned it.

    Feb 26, 2016 at 22:04

  • 3

    if TextView with width and height equal “wrap_content” is inside LinearLayout with width and height “match_parent” then you need to set the layout_gravity of Linear_layout.

    Feb 1, 2017 at 16:08

  • 2

    @MooingDuck, when height is “wrap_content” and gravity is “center_vertical”, it draws in Design (preview) as centered, but in a device it is positioned to a top. So make height “match_parent” and set gravity to “center_vertical” or “center”.

    – CoolMind

    Feb 28, 2018 at 8:45

312

You can also set it up dynamically using:

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

4

  • 145

    Or just textview.setGravity(Gravity.CENTER);

    – Amplify91

    Apr 7, 2011 at 16:00

  • 1

    What is the criteria for judgement to decide which way to choose, to write in xml or write in Java file?

    – kenju

    Aug 20, 2015 at 8:49

  • 3

    @Kenju if you want it to be easily editable and the value won’t change during runtime: XML. If you must change the value realtime (for example, change the alignment when the user presses a button) then you can only do it via Java.

    Nov 6, 2015 at 0:38

  • 3

    @GustavoMaciel so, basically xml unless it needs to be modified dynamically … Now i got it. Thanks for your simple and useful answer !

    – kenju

    Nov 6, 2015 at 0:46