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

75

2325

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?

Lied answered 11/1, 2009 at 0:27 Comment(2)
set both layout_width and layout_height to fill_parent, then set gravity to center. That'll do the trickMancino
fill_parent is deprecated now so use MATCH_PARENT in layout_width and layout_height and set gravity of the TextView to center.Sturgeon
U
3426

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

Uraemia answered 11/1, 2009 at 1:53 Comment(16)
This doesn't work when used with a RelativeLayout where the layout's height & width are set to wrap_contentTownley
@Rob, if the width and height are wrap_content, then technically, the text is already centered.Lauraine
If I want to align TextView relative to another view, but it's text centered in itself?Outspan
If you're a minimalist like me you can drop the layout tags.Afraid
This isn't working for me (android:gravity="center"). It still shows at far left of screen. :(Nakashima
it sounds like your Textviews width and height are set to wrap_content. If this is the case you can set layout_gravity to centerLashandralashar
Not to be confused with android:layout_gravity="center", which does something else.Eternal
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.Hushaby
gravity=center not working. Textview height=34dp and width=100dp.Please helpCryptoanalysis
Don't forget, there is also center_veritcal and center_horizontal.Tunicate
@ Rob,relative layout provides android:layout_centerInParent="true"Jesselyn
you can also set manually the height and width to certain value to implement gravity.Cindiecindra
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)Iscariot
The key is android:layout_height="match_parent" and android:gravity="center". If height is "wrap_content", it won't work.Eton
That centers the TextView, the question is how to center the text inside the TextView and I believe the answer is:- android:textAlignment="center"Courtmartial
Technically, the text is already centered if the width and height are wrap_content.Isochromatic
M
489
android:gravity="center" 

This will do the trick

Magnesium answered 8/2, 2010 at 22:54 Comment(6)
The textview has to be match_parent or fill_parent for this to work. If its wrap content, then it will not center.Phosgenite
@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.Unbuild
@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.Phosgenite
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.Hospers
@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".Eton
the question is how to center the text inside the TextView and I believe the answer is:- android:textAlignment="center"Zwickau
E
315

You can also set it up dynamically using:

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
Erection answered 21/11, 2009 at 12:25 Comment(4)
Or just textview.setGravity(Gravity.CENTER);Faunia
What is the criteria for judgement to decide which way to choose, to write in xml or write in Java file?Unworldly
@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.Longevity
@GustavoMaciel so, basically xml unless it needs to be modified dynamically ... Now i got it. Thanks for your simple and useful answer !Unworldly
D
127
android:layout_centerInParent="true"

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

Dhar answered 11/11, 2011 at 0:58 Comment(2)
This centers the TextView, not the text in it.Rudman
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.Nodababus
I
89

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

Indigent answered 1/1, 2013 at 20:11 Comment(0)
B
59

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"        
/>
Beforetime answered 30/5, 2013 at 5:34 Comment(0)
B
46

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.

Biradial answered 17/10, 2014 at 12:11 Comment(0)
H
41

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>
Hail answered 13/5, 2011 at 11:57 Comment(1)
Interesting. Have to check if that is valid for LinearLayout (the TableRow parent) as well.Overawe
S
39

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);
Susiesuslik answered 15/5, 2012 at 5:3 Comment(2)
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).Knighthead
layout_* tells the View's parent where it wants to be. Without layout_* tells the View how to place its componentsOza
P
34

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.

Pontias answered 16/7, 2012 at 4:28 Comment(0)
W
33

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);
Weirdie answered 3/1, 2015 at 3:58 Comment(0)
M
25

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);
Marko answered 4/10, 2013 at 17:18 Comment(0)
S
22

Use this for relative layout

android:layout_centerInParent="true"

and for other layout

android:gravity="center" 
Subfamily answered 11/10, 2013 at 11:8 Comment(0)
C
20

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>
Chemise answered 17/12, 2015 at 6:12 Comment(0)
S
17

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.

Staff answered 24/8, 2013 at 20:50 Comment(2)
The difference is described here: #16196944Psia
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.Paramedic
A
14

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>
Aberrant answered 21/11, 2012 at 3:15 Comment(0)
T
12

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"
         />
Trussell answered 5/6, 2019 at 1:9 Comment(3)
textAlignment="center" won't do the job until the TextView has dimensions to match_parentImbecilic
@Imbecilic above example is working without any issues.Trussell
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 itselfImbecilic
B
11

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

Berdichev answered 2/4, 2013 at 3:34 Comment(0)
H
11

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

textview.setGravity(Gravity.CENTER)
Hydroxyl answered 15/12, 2015 at 12:38 Comment(0)
C
11

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>
Coff answered 20/9, 2017 at 12:8 Comment(2)
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.Nmr
Thank you for android:gravity="center_horizontal" and android:layout_gravity="center_vertical"Locarno
B
10

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>
Batholith answered 15/7, 2012 at 2:14 Comment(0)
P
10

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"/>
Putnam answered 15/11, 2015 at 6:33 Comment(0)
L
10

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>
Lusaka answered 17/6, 2016 at 18:29 Comment(0)
G
9

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" />
Gabriel answered 3/6, 2013 at 9:54 Comment(0)
H
9

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);
Heelandtoe answered 7/3, 2016 at 13:42 Comment(0)
C
9

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"
/>
Cutin answered 13/2, 2020 at 7:30 Comment(1)
Yes, android:includeFontPadding="false" removes small paddings around the text.Eton
U
8

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"
Uptown answered 4/7, 2013 at 20:9 Comment(0)
J
8

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);
Junia answered 10/2, 2018 at 16:5 Comment(0)
C
7

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

<TextView
    android:gravity="center" />
Canikin answered 6/5, 2013 at 17:56 Comment(0)
H
7

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" />
Hiett answered 20/5, 2016 at 10:18 Comment(0)
F
7

You can do like this to get text centered

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />
Fakieh answered 30/6, 2017 at 9:27 Comment(0)
T
5

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

Tl answered 4/9, 2016 at 19:33 Comment(0)
I
4

I don't think, You really need to do that just define your TextView in layout file like this

<RelativeLayout
    android:layout_width="width"
    android:layout_height="height">
    <TextView
        android:id="@+id/yourid"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your text" />
</RelativeLayout>
Impermeable answered 23/1, 2017 at 12:9 Comment(0)
T
4

just add this in your xml.

android:gravity="center" 
Tahr answered 9/9, 2022 at 12:58 Comment(0)
U
4

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
Unexacting answered 18/6, 2023 at 11:37 Comment(0)
R
3

Set the gravity attribute in the layout file as android:gravity="center"

Roswald answered 17/8, 2016 at 22:29 Comment(0)
A
3
 <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>
Actinochemistry answered 25/3, 2019 at 12:2 Comment(0)
C
3

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"/> 
Cakewalk answered 1/4, 2019 at 7:12 Comment(0)
L
3

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
Legit answered 17/8, 2020 at 8:49 Comment(0)
I
3

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"

Imre answered 3/2, 2022 at 12:38 Comment(0)
S
2
<TextView
android:id="+@id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
Sharell answered 19/2, 2014 at 12:55 Comment(0)
E
2

Did you try this way?

<TextView
android:id="+@id/txtVw"
android:gravity="center"
/>
Enchorial answered 13/3, 2014 at 12:57 Comment(0)
K
2

use the below code in xml it worked for me you can change the orientation it will be in the centre

<LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:background="@color/colorPrimaryDark"
        android:gravity="center"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/id_text"
            android:layout_width="wrap_content"
            android:textSize="18sp"
            android:textColor="@android:color/white"
            android:textStyle="normal"
            android:layout_height="wrap_content"
            android:text="centre me"/>
    </LinearLayout>
Keeper answered 7/12, 2016 at 10:26 Comment(0)
R
2

Use android:gravity="center" to resolve your issue.

Ringmaster answered 22/3, 2017 at 8:9 Comment(0)
R
2
<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);
Resistless answered 22/4, 2018 at 4:41 Comment(0)
P
2
android:textAlignment="center"

this will do the trick

Pinsky answered 9/10, 2021 at 4:35 Comment(1)
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: stackoverflow.com/help/how-to-answer . Good luck 🙂Piccadilly
I
2

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" 
Irick answered 6/5, 2022 at 8:26 Comment(0)
F
1

If you are working with RelativeLayout, try using this property inside your TextView tag :

android:layout_centerInParent= true
Fourposter answered 5/3, 2017 at 15:21 Comment(1)
The "true" should be in quotesCoryphaeus
A
1

try this :

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
/>
Abbotsen answered 20/9, 2017 at 3:34 Comment(0)
B
1

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/>
Brie answered 3/12, 2017 at 18:5 Comment(0)
I
1

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"
    />
Intersidereal answered 5/2, 2018 at 10:43 Comment(0)
F
1

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);
Farrica answered 3/3, 2018 at 19:33 Comment(0)
S
1

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" />
Searchlight answered 13/6, 2018 at 5:50 Comment(0)
A
1
android:gravity="center" 

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

Altissimo answered 29/12, 2018 at 13:35 Comment(0)
F
1

You must set the Gravity like this:

(textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
Fonteyn answered 7/2, 2019 at 4:24 Comment(0)
F
1

Try this:

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" />
Fonteyn answered 16/2, 2019 at 5:8 Comment(0)
W
1

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

Windswept answered 26/3, 2019 at 5:44 Comment(0)
E
1

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" />
Euler answered 26/8, 2020 at 16:30 Comment(0)
G
1

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"

Grownup answered 7/11, 2021 at 3:35 Comment(0)
P
0
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Tours"
        android:id="@+id/textView" />
Projectile answered 20/7, 2016 at 6:27 Comment(0)
B
0

And for those wondering how to center it vertically in its TextView element while keeping it on the left (horizontally) :

android:gravity="center"
android:textAlignment="viewStart"
Bodily answered 17/8, 2016 at 21:46 Comment(0)
P
0
There are 2 ways to do this :

1. From Xml
     use :  
        <TextView
         .........
        android:gravity="center"/>

2. From Java class
    use : 

     [textview].setGravity(Gravity.CENTER);
Pyle answered 6/10, 2016 at 11:8 Comment(0)
A
0

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" />
Altissimo answered 13/11, 2017 at 10:42 Comment(0)
L
0

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)

Luciolucita answered 27/11, 2017 at 19:4 Comment(0)
G
0

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" />
Grimalkin answered 14/12, 2017 at 16:58 Comment(0)
T
0

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"
    />
Triste answered 18/12, 2017 at 7:6 Comment(0)
D
0

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

android:gravity="center"
Dirac answered 20/3, 2018 at 15:10 Comment(0)
G
0

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>
Gypsophila answered 8/5, 2018 at 9:25 Comment(0)
B
0

Here is solution.

<TextView  
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center"
       android:text="**Your String Value**" />
Bandoleer answered 25/5, 2018 at 8:12 Comment(0)
H
0

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"

Hereabouts answered 5/11, 2018 at 11:12 Comment(0)
P
0

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

Plasticizer answered 14/12, 2018 at 16:56 Comment(0)
B
0

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

Blatman answered 27/4, 2019 at 7:6 Comment(0)
U
0

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.

Ultima answered 10/5, 2019 at 19:13 Comment(0)
U
0

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" />
Ursulina answered 2/12, 2022 at 7:4 Comment(0)
V
0

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.

Venter answered 4/9, 2023 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.