Fiocchi Luca
Fiocchi Luca

Reputation: 21

Center a TextView: Why it doesn't work?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:textColor="@color/red"
    android:textStyle="bold"
    android:text="@string/desc" 
    />

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:textStyle="bold|italic"
    android:text="Welcome"
    android:textAppearance="?android:attr/textAppearanceLarge"
     />

</RelativeLayout>

I would like to center the second TextView but I can not ... This is the code, you know help me? Where is the error? I'm going crazy!

Upvotes: 0

Views: 1168

Answers (5)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// try this 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="60dp"
        android:textStyle="bold|italic"
        android:text="Welcome"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_above="@id/textView2"
        android:layout_marginTop="20dp"
        android:textColor="@color/red"
        android:text="@string/desc"
        android:textStyle="bold"/>

</RelativeLayout>

Upvotes: 0

JWqvist
JWqvist

Reputation: 717

Change your layout like this

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout2"
    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_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:textColor="@color/red"
    android:textStyle="bold"
    android:text="@string/desc" 
    />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginTop="60dp"
    android:textStyle="bold|italic"   
    android:text="Welcome"
    android:textAppearance="?android:attr/textAppearanceLarge"
     />

</RelativeLayout>

Always use match_parent instead of fill_parent

Upvotes: 0

Mohit
Mohit

Reputation: 11314

What about setting it this way

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"        >
    <TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:textColor="@color/red"
    android:textStyle="bold"
    android:text="@string/desc"         />

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:textStyle="bold|italic"
    android:text="Welcome"
    android:textAppearance="?android:attr/textAppearanceLarge"         />

</RelativeLayout>

Alternatively

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout2"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:layout_height="fill_parent">
        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_contet"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"           
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:textColor="@color/red"
        android:textStyle="bold"
        android:text="@string/desc"             />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"           
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:textStyle="bold|italic"
        android:text="Welcome"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>

Upvotes: 0

Prakhar
Prakhar

Reputation: 2310

try this :

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:textStyle="bold|italic"
    android:text="Welcome"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
     />

Upvotes: 0

gunar
gunar

Reputation: 14710

Add the following to your textview:

android:gravity="center"

But at the same time I would recommend to reconsider your layout as you're having 2 textviews taking the full screen.

Edit center centers both on horizontal and vertical. If you need only on a specific axis, use one of: android:gravity="center_horizontal" or android:gravity="center_vertical"

Upvotes: 5

Related Questions