Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 342
Riskhan Avatar asked Feb 03 '12 05:02

Riskhan


People also ask

How do I set TextView in center in RelativeLayout?

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" . Notice that this won't work if a RelativeLayout contains an attribute android:gravity="center_horizontal".

How do I center TextView in XML?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

How do you center a button in RelativeLayout?

If you have a single Button in your Activity and you want to center it the simplest way is to use a RelativeLayout and set the android:layout_centerInParent=”true” property on the Button.

How do you center align text in relative layout?

If you want to make it center then use android:layout_centerVertical="true" in the TextView.


2 Answers

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

like image 123
Brian Dupuis Avatar answered Sep 25 '22 00:09

Brian Dupuis


Use this:

android:layout_centerHorizontal="true" 

within TextView

like image 33
Akram Avatar answered Sep 26 '22 00:09

Akram