Rectangle 27 0

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


.setGravity(Gravity.CENTER);
<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
    android:text="@string/**yourtextstring**"
/>
android:gravity="center"

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

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

I'm assuming you're using XML layout.

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

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

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

Note
Rectangle 27 0

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


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

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

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.

There are two ways of doing this.

Note
Rectangle 27 0

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


.setGravity(Gravity.CENTER);
<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
    android:text="@string/**yourtextstring**"
/>
android:gravity="center"

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

I'm assuming you're using XML layout.

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

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

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

Note
Rectangle 27 0

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


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

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.

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

Note
Rectangle 27 0

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


android:gravity="center"

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

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

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

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.

Note
Rectangle 27 0

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


android:gravity="center"

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

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

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

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.

Note
Rectangle 27 0

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


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

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.

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

Note
Rectangle 27 0

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


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").

You can also use the combination:

Note
Rectangle 27 0

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


<TextView      
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"
    android:text="@string/Hello_World"        
/>
TextView txtView = (TextView) findViewById(R.id.txtView);
txtView.setGravity(Gravity.CENTER_HORIZONTAL);
txtView.setGravity(Gravity.CENTER_VERTICAL);
Note
Rectangle 27 0

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


<TextView      
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"
    android:text="@string/Hello_World"        
/>
TextView txtView = (TextView) findViewById(R.id.txtView);
txtView.setGravity(Gravity.CENTER_HORIZONTAL);
txtView.setGravity(Gravity.CENTER_VERTICAL);
Note