Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView in the center of the screen [duplicate]

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?

like image 846
Mun0n Avatar asked Jan 16 '12 23:01

Mun0n


3 Answers

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"
like image 105
mdelolmo Avatar answered Nov 11 '22 11:11

mdelolmo


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

like image 21
Muhammad Nabeel Arif Avatar answered Nov 11 '22 10:11

Muhammad Nabeel Arif


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

like image 15
Rotemmiz Avatar answered Nov 11 '22 11:11

Rotemmiz