39

Possible Duplicate:
How do I center text horizontally and vertical in a TextView in Android?

I have a RelativeLayout (before it was a LinerLayout), that occupies all the screen and I want to put in the center of this Layout, a TextView. I try to do it with gravity = "center" layout_gravity = "center" and a few more, but it doesn't work.

Anybody knows how to center the TextView in the middle of the screen?

EDIT

Ok, I think I explained badly. I think the TextView is in the center, but what I want to center is the text in the TextView. Can I do this?

Community
  • 1
  • 1
Mun0n
  • 4,438
  • 4
  • 28
  • 46
  • try to see here http://stackoverflow.com/questions/2784003/how-do-you-center-a-textview-in-layout it's was always problem.. i don't know xml.. can't help u. but this i always fixed by code.. and never found easy way to do it as i doing it (cos i don't know xml ) – Peter Jan 16 '12 at 23:27
  • Center both horizontally and vertically? Edit: Rotemmiz beat me to the punch lol. He posted what you're looking for. – bschultz Jan 16 '12 at 23:28
  • You should add android:gravity="center" as in my answer. – dcanh121 Jan 19 '12 at 19:49
  • Use android:gravity="center" – Pihu Dec 19 '13 at 05:05
  • If you are working under RelativeLayout use android:layout_centerVertical="true" and android:layout_centerHorizontal="true" with your TextView of android:layout_width="wrap_content", if you use android:layout_width="match_parent" , use android:gravity="center" – Kushal Ramola Jun 02 '17 at 06:30

9 Answers9

69

If your font size is big enough, it might look like it's not centered, because of the font padding.
Try using the already mentioned properties combined with android:includeFontPadding, something like this:

    android:gravity="center"
    android:includeFontPadding="false"
mdelolmo
  • 6,417
  • 3
  • 40
  • 58
18

If you want to align contents of RelativeLayout in the center then you need to put android:gravity="center" in the RelativeLayout's properties. Below is a sample XML code for it along with its visual representation.

XML Code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:background="@color/white">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="My Text View"
            android:textColor="@color/black"
            android:textSize="25dp"
            android:textStyle="bold" />
</RelativeLayout>

Graphical Layout:

enter image description here

Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
15

Try android:layout_centerInParent="true" or android:layout_centerHorizontal="true" these apply to RelativeLayout

Mun0n
  • 4,438
  • 4
  • 28
  • 46
Rotemmiz
  • 7,933
  • 3
  • 36
  • 36
  • 2
    The answer here is what you're looking for and will get the TextView object centered within the layout, but you may also still need to add android:gravity="center" so that the text will be centered within the TextView object. – Aldryd Jan 16 '12 at 23:36
  • It doesn't work. My textSize is too large and I don't know how to center it. – Mun0n Jan 17 '12 at 16:04
  • Try posting the xml layout, I will be able to help you further more... – Rotemmiz Jan 17 '12 at 17:47
2

You can also set the gravity="center" of its parent.

Zain
  • 37,492
  • 7
  • 60
  • 84
deepak Sharma
  • 1,641
  • 10
  • 23
1
android:gravity="center" 

works on RelativeLayouts only. So try

<TextView
        android:id="@+id/title"
        android:layout_width="fill_parent" <!--relative Layout-->
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Mint Payments"/>
gsb
  • 5,520
  • 8
  • 49
  • 76
1

Alternatively, taken from another post

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/**yourtextstring**" />
Community
  • 1
  • 1
Orkun
  • 6,998
  • 8
  • 56
  • 103
0

This should display in center of the screen with center aligned. If you want to text alignment, you may use android:gravity="right" or android:gravity="left"

     <RelativeLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:gravity="center" 
                    android:text="This is some text" />    
    </RelativeLayout>
dcanh121
  • 4,665
  • 11
  • 37
  • 84
0
    <RelativeLayout
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
     <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Txt is aligned in Center" />
   </RelativeLayout>
VenomVendor
  • 15,064
  • 13
  • 65
  • 96
-2

use

android:gravity="center"

attribute for your TextView in layout_XML File.

Lavakush
  • 1,910
  • 2
  • 11
  • 14