user971741
user971741

Reputation:

Align a TextView inside a RelativeLayout Horizontally only

I want o align my TextView inside a RelativeLayout Horizontally only in the middle. Tried all the options as you can see below but none seem to make a difference.

Only thing that makes a differnce is android:layout_centerInParent="true" But issue with this is it also center it vertically however i only want it horizontally.

Please suggest.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relativeLayoutFragment"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@+id/userLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/marginOne"
            android:background="#80000000"
            android:gravity="center"
            android:padding="@dimen/marginOne"
            android:text="@string/findingLocation"
            android:textColor="#FFFFFF"
            android:textSize="@dimen/FontTwo" />

    </RelativeLayout>

Upvotes: 0

Views: 45

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

You need to add gravity in you Relative layout to android:gravity="center_horizontal"

sample:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayoutFragment"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" 
    android:gravity="center_horizontal">

    <TextView
        android:id="@+id/userLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/marginOne"
        android:background="#80000000"
        android:gravity="center"
        android:padding="1dp"
        android:text="I am here"
        android:textColor="#FFFFFF"
        android:textSize="40sp" />

</RelativeLayout>

result:

enter image description here

Upvotes: 1

Related Questions