Android: Centering text in a textview when texview is centered in a layout

I need the text to be in the direct center of the screen, and want the textview to fill the parent. but it keeps aligning the start of the text to the center of the screen.

How do i center the text in the middle of the screen when using a textview in android?

this is what i have

this is the xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"  //removing this line has no effect
        android:gravity="center"
        android:text="TextView" />
</RelativeLayout>

-------------------------------------

Answers


If you have to use a RelativeLayout here your solution:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="TextView" />

</RelativeLayout>

You can also use a LinearLayout if you want:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="TextView" />

</LinearLayout>

Try this

<?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:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="TextView" />

</RelativeLayout>

Or

<?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" 
    android:gravity="center">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="TextView" />

</RelativeLayout>

I think you need to save the file activity_main.xml* first to see the changes(CTRL + S) it isn't saved, second the layout is how it would look on Nexus one, you better create separate layout-folders for 600dp+. Third there's some error I can see on your console please eradicate it, If nothing works clean your project and restart your eclipse I guess it would work


Need Your Help

What does a typical ./configure do in Linux?

linux build makefile configure

Why is it necessary though everything is specified in a makefile ?