How to align text inside TextView in Android?

I have a multi-line TextView in Android. This is a child of LinearLayout. It is already horizontal, and I also want the text to be inside it.

Here is what I have now:

_ _ _ _ _ _| aaaaaaaaaaaaaaaaaaaaa _ _ _ _ _ _| aaaaaaaa _ _ _ _ _ _| aaaaaaaaaaaaaa 

I want to:

 _ _ _ _ _ _| aaaaaaaaaaaaaaaaaaaaa _ _ _ _ _ _| aaaaaaaa _ _ _ _ _ _| aaaaaaaaaaaaaa 

Where:

_ _ _ _ = space on the side of the TextView

| = edge of TextView

+54
android textview android-textview android-gui
Jun 19 '12 at 7:13
source share
4 answers

Have you tried the gravity attribute on a TextView ?

  android:gravity="center" 

And don't forget to use android:gravity , not android:layout_gravity .

+148
Jun 19 '12 at 7:15
source share

android:gravity="center_horizontal" should do the android:gravity="center_horizontal" thing.

More information can be found here or here .

+13
Jun 19 '12 at 7:16
source share

Yes, use the android: gravity attribute to reposition the data inside the views.

+1
Jun 07 '17 at 13:15
source share

Make the text (which you want to set in the TextView) in strings.xml as follows:

 <string name="content"> <center>Your text goes here.</center> </string> 

and in the TextView add the following attribute:

 <TextView ... android:text="@string/content"/> 

It will center the text in the TextView horizontally.

-one
Aug 13 '18 at 14:41
source share



All Articles