TextView in the center of the screen

52,874

Solution 1

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"

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

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"/>
Share:
52,874
Mun0n
Author by

Mun0n

Android Developer and Drunkcode Co-founder StackOverflow is the best site to do Copy Paste and share some kwnoledge Drunkcode o_O

Updated on June 20, 2021

Comments

  • Mun0n
    Mun0n almost 3 years

    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?

  • Aldryd
    Aldryd over 12 years
    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.
  • Mun0n
    Mun0n over 12 years
    I tried this, and doesn't work, the text don't center.
  • Orkun Ozen
    Orkun Ozen over 12 years
    What layout do you have? Cos it did the trick with linear layout for me.
  • Mun0n
    Mun0n over 12 years
    I have RelativeLayout, I will try with LinearLayout and I will tell you.
  • Orkun Ozen
    Orkun Ozen over 12 years
    i wonder if the other approach works for every other layout.
  • Mun0n
    Mun0n over 12 years
    It doesn't work. My textSize is too large and I don't know how to center it.
  • Rotemmiz
    Rotemmiz over 12 years
    Try posting the xml layout, I will be able to help you further more...
  • Mun0n
    Mun0n over 12 years
    thanks @mdelolmo, is just what I need!
  • Mun0n
    Mun0n over 12 years
    it is wrong because you don't understand the question. I test it and doesn't work. @mdelolm knows how to do it.
  • Jin35
    Jin35 over 12 years
    Give bounty please, your question are still in featured :)
  • Ruchir Baronia
    Ruchir Baronia over 8 years
    What does android:includeFontPadding="false" do?
  • mdelolmo
    mdelolmo over 8 years
    @RuchirBaronia, it means Android will take into account the font "white space" that it has above and below, to calculate its position in the layout.
  • Michael Fulton
    Michael Fulton almost 8 years
    This is what I needed +1
  • Admin
    Admin about 7 years
    this command is false
  • Lavakush
    Lavakush about 7 years
    android:gravity will work here.
  • MeqDotNet
    MeqDotNet almost 5 years
    works like a charm thank you @mdelolmo