TextView horizontal center in RelativeLayout

AndroidAndroid Layout

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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".

Solution 2 - Android

Use this:

android:layout_centerHorizontal="true"

within TextView

Solution 3 - Android

<?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>

Solution 4 - Android

use layout_centerInParent="true"(layout_centerInHorizontal,layout_centerInVertical)

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

Solution 5 - Android

replace textview with below code

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

Solution 6 - Android

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRiskhanView Question on Stackoverflow
Solution 1 - AndroidBrian DupuisView Answer on Stackoverflow
Solution 2 - AndroidAkramView Answer on Stackoverflow
Solution 3 - AndroidRajeswari KotikalapudiView Answer on Stackoverflow
Solution 4 - AndroidJohnCookieView Answer on Stackoverflow
Solution 5 - AndroidSathishBabu SView Answer on Stackoverflow
Solution 6 - AndroidParag ChauhanView Answer on Stackoverflow