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
?
-
6set both layout_width and layout_height to fill_parent, then set gravity to center. That'll do the trick – Amila Jun 25 '15 at 9:39
-
fill_parent is deprecated now so use MATCH_PARENT in layout_width and layout_height and set gravity of the TextView to center. – Bilal Ahmad Jun 30 '18 at 9:58
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>
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>
<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>
<TextView
android:id="+@id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
Did you try this way?
<TextView
android:id="+@id/txtVw"
android:gravity="center"
/>
<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);
<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
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"/>
If you are working with RelativeLayout
, try using this property inside your TextView
tag :
android:layout_centerInParent= true
try this :
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
/>
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"
/>
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);
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" />
You must set the Gravity like this:
(textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
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
<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" />
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"
There are 2 ways to do this :
1. From Xml
use :
<TextView
.........
android:gravity="center"/>
2. From Java class
use :
[textview].setGravity(Gravity.CENTER);
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" />
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)
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/>
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
<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”
<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"
( 4 ) Center Text Horizontally & Vertically
android:gravity=”center” this will center the text horizontally and vertically
<TextView android:layout_width="fill_parent" android:layout_height="100dp" android:text="Hello World!" android:gravity="center" android:background="#333" android:textColor="#fff" />
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" />
Use Gravity Property in the TextView to center the Text Vertically and Horizontally.
android:gravity="center"
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>
Here is solution.
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="**Your String Value**" />
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"
You can programatically do this in java with: textview.setGravity(Gravity.CENTER);