Horizontal Center TextView in RelativeLayout

In my application screen, I want to show Heading as a horizontal center. I tried using the XML codes below

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

thank

+46
android android-layout
03 Feb '12 at 5:23
source share
6 answers

android:gravity controls the appearance in a TextView. Therefore, if you have two lines of text, they will be centered within the TextView. Try android:layout_centerHorizontal="true" .

+110
Feb 03 2018-12-12T00:
source share

Use this:

 android:layout_centerHorizontal="true" 

inside textview

+15
03 Feb 2018-12-12T00:
source share
 <?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> 
+5
Feb 03 2018-12-12T00:
source share

use layout_centerInParent="true"(layout_centerInHorizontal,layout_centerInVertical)

gravity means algin text in text mode, not the view itself

+4
Feb 03 '12 at 5:27
source share

replace textview with code below

 <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:text="Connections" /> 
+4
03 Feb 2018-12-12T00:
source share

add only 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> 
+1
Feb 03 2018-12-12T00:
source share



All Articles