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

Asked 2023-09-20 20:33:55 View 82,673

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?

  • set both layout_width and layout_height to fill_parent, then set gravity to center. That'll do the trick - anyone
  • fill_parent is deprecated now so use MATCH_PARENT in layout_width and layout_height and set gravity of the TextView to center. - anyone

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

Answered   2023-09-20 20:33:55

  • This doesn't work when used with a RelativeLayout where the layout's height & width are set to wrap_content - anyone
  • @Rob, if the width and height are wrap_content, then technically, the text is already centered. - anyone
  • If I want to align TextView relative to another view, but it's text centered in itself? - anyone
  • This isn't working for me (android:gravity="center"). It still shows at far left of screen. :( - anyone
  • Not to be confused with android:layout_gravity="center", which does something else. - anyone
android:gravity="center" 

This will do the trick

Answered   2023-09-20 20:33:55

  • The textview has to be match_parent or fill_parent for this to work. If its wrap content, then it will not center. - anyone
  • @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. - anyone
  • @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. - anyone
  • 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. - anyone
  • @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". - anyone

You can also set it up dynamically using:

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

Answered   2023-09-20 20:33:55

  • Or just textview.setGravity(Gravity.CENTER); - anyone
  • What is the criteria for judgement to decide which way to choose, to write in xml or write in Java file? - anyone
  • @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. - anyone
  • @GustavoMaciel so, basically xml unless it needs to be modified dynamically ... Now i got it. Thanks for your simple and useful answer ! - anyone
android:layout_centerInParent="true"

This works when used with a RelativeLayout where the layout's height & width are set to wrap_content.

Answered   2023-09-20 20:33:55

  • This centers the TextView, not the text in it. - anyone
  • This does NOT work when there is word-wrap in the TextView. The text will be full-width and multi-line AND LEFT-justified within the TextView. It will appear left-justified on the display. You should use "gravity" as specified in the other answers. - anyone

You can also use the combination:

android:gravity="left|center"

Then, if textview width is more than "fill_parent" the text will still be aligned to left (not centered as with gravity set only to "center").

Answered   2023-09-20 20:33:55

Apply gravity:

TextView txtView = (TextView) findViewById(R.id.txtView);
txtView.setGravity(Gravity.CENTER_HORIZONTAL);

For vertical:

txtView.setGravity(Gravity.CENTER_VERTICAL);

In XML:

<TextView      
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"
    android:text="@string/Hello_World"        
/>

Answered   2023-09-20 20:33:55

There are two ways of doing this.

The first in the XML code. You need to pay attention at the Gravity Attribute. You also can find this attribute in the Graphic Editor; it may be easier than the XML EDITOR.

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical|center_horizontal"
    android:text="Your Text"
/>

For your specific scenario, the values of gravity will be:

center_vertical|center_horizontal

In the graphical editor you will find all the possible values, even see their results.

Answered   2023-09-20 20:33:55

If you are using TableLayout make sure to set the gravity of the TableRows to center, too. Otherwise it will not work. At least it didn't work with me until I set the gravity of the TableRow to center.

For example, like this:

<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center">        
    <TextView android:text="@string/chf" android:id="@+id/tv_chf" android:layout_weight="2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center"></TextView>        
</TableRow>

Answered   2023-09-20 20:33:55

  • Interesting. Have to check if that is valid for LinearLayout (the TableRow parent) as well. - anyone

You need to set TextView Gravity (Center Horizontal & Center Vertical) like this:

android:layout_centerHorizontal="true"   

and

android:layout_centerVertical="true"

And dynamically using:

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

Answered   2023-09-20 20:33:55

  • Code semantics is wrong. Replace layout_centerhorizontal and layout_centervertical by layout_centerHorizontal and layout_centerVertical ("H" and "V" to uppercase, otherwise it wont work). - anyone
  • layout_* tells the View's parent where it wants to be. Without layout_* tells the View how to place its components - anyone

In my opinion,

android:gravity="center"

is better than,

android:layout_centerInParent="true"

which is better than,

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

at least for formatting text.

Answered   2023-09-20 20:33:55

For Linear Layout: In XML use something like this

<TextView  
    android:id="@+id/textView1"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical|center_horizontal"
    android:text="Your Text goes here"
/>

To do this at run time use something like this in your activity

TextView textView1 =(TextView)findViewById(R.id.texView1);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

For Relative Layout: in XML use some thing like this

<TextView  
    android:id="@+id/textView1"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerInParent="true"
    android:text="Your Text goes here"
/>

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

Answered   2023-09-20 20:33:55

Use in the XML file.

Layout file

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:text="@string/stringtext"/>

or:

Use this inside the Java class

TextView textView =(TextView)findViewById(R.id.texviewid);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

Answered   2023-09-20 20:33:55

Use this for relative layout

android:layout_centerInParent="true"

and for other layout

android:gravity="center" 

Answered   2023-09-20 20:33:55

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>

Answered   2023-09-20 20:33:55

While using gravity works for TextView, there's an alternate method implemented in API level 17 -

textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

Don't know the difference, but it works too. However only for API level 17 or higher.

Answered   2023-09-20 20:33:55

  • The difference is described here: stackoverflow.com/questions/16196444/… - anyone
  • This will truly center the text in the TextView. If you just use gravity, there's a bit of trailing padding depending on what the text is.... If you have a little circle badge with text in the center, for instance, you can see a very obvious difference. - anyone

In RelativeLayout, it will be nice with it.

And another Button and anything else you can add.

The following works nicely for me.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff314859"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">
    <TextView 
        android:id="@+id/txt_logo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="your text here"
        android:textSize="30dp"
        android:gravity="center"/>

        ...other button or anything else...

</RelativeLayout>

Answered   2023-09-20 20:33:55

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

Answered   2023-09-20 20:33:55

  • textAlignment="center" won't do the job until the TextView has dimensions to match_parent - anyone
  • @SAurabh above example is working without any issues. - anyone
  • 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 - anyone

Easiest way (which is surprisingly only mentioned in comments, hence why I am posting as an answer) is:

textview.setGravity(Gravity.CENTER)

Answered   2023-09-20 20:33:55

You can just set the gravity of your textview into CENTER.

Answered   2023-09-20 20:33:55

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>

Answered   2023-09-20 20:33: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. - anyone
  • Thank you for android:gravity="center_horizontal" and android:layout_gravity="center_vertical" - anyone

If you are trying to center text on a TableRow in a TableLayout, here is how I achieved this:

<TableRow android:id="@+id/rowName"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="5dip" >
    <TextView android:id="@+id/lblSomeLabel"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:gravity="center"
              android:layout_width="0dp"
              android:layout_weight="100"
              android:text="Your Text Here" />
</TableRow>

Answered   2023-09-20 20:33:55

If you are using Relative Layout:

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/stringname"
    android:layout_centerInParent="true"/>

If you are using LinearLayout

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/stringname"
    android:layout_gravity="center"/>

Answered   2023-09-20 20:33:55

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>

Answered   2023-09-20 20:33:55

Here is my answer that I had used in my app. It shows text in center of the screen.

<TextView
    android:id="@+id/txtSubject"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/subject"
    android:layout_margin="10dp"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge" />

Answered   2023-09-20 20:33:55

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

Answered   2023-09-20 20:33:55

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

Answered   2023-09-20 20:33:55

As many answers suggest above works fine.

android:gravity="center"

If you want to center it just vertically:

android:gravity="center_vertical"

or just horizontally:

android:gravity="center_horizontal"

Answered   2023-09-20 20:33:55

Simply, in your XML file, set the textview gravity to center:

<TextView
    android:gravity="center" />

Answered   2023-09-20 20:33:55

android:gravity="center_horizontal" for align text Center horizontally. android:gravity="center_vertical" for align text Center vertically. android:gravity="center" for align text Center both vertically and horizontally.

<TextView
        android:layout_width="match_parent"
        android:gravity="center_horizontal|center_vertical"
        android:layout_height="match_parent" />

Answered   2023-09-20 20:33:55

You can do like this to get text centered

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

Answered   2023-09-20 20:33:55