Riskhan
Riskhan

Reputation: 4470

TextView horizontal center in RelativeLayout

In my app screen, i want to show Heading as horizontally center. I tried with below layout xml codes

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_marginTop="10dip"
  >
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="Connections" />
</RelativeLayout>

thanks

Upvotes: 71

Views: 87523

Answers (6)

Akram
Akram

Reputation: 7526

Use this:

android:layout_centerHorizontal="true"

within TextView

Upvotes: 19

SathishBabu S
SathishBabu S

Reputation: 372

replace textview with below code

<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:text="Connections" />

Upvotes: 3

Rajeswari Kotikalapudi
Rajeswari Kotikalapudi

Reputation: 592

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_marginTop="10dip"
    >
  <TextView
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Connections" />
</RelativeLayout>

Upvotes: 7

Parag Chauhan
Parag Chauhan

Reputation: 35936

only add this android:gravity="center_horizontal"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_marginTop="10dip" 
  android:gravity="center_horizontal"
  >
  <TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="Connections" />
</RelativeLayout>

Upvotes: 2

Brian Dupuis
Brian Dupuis

Reputation: 8176

android:gravity controls the appearance within the TextView. So if you had two lines of text they would be centered within the bounds of the TextView. Try android:layout_centerHorizontal="true".

Upvotes: 162

JohnCookie
JohnCookie

Reputation: 671

use layout_centerInParent="true"(layout_centerInHorizontal,layout_centerInVertical)

gravity means the algin of text on textview, not the view itself

Upvotes: 5

Related Questions