How to align the text inside the TextView in Android?

user552400 picture user552400 · Jun 19, 2012 · Viewed 90.3k times · Source

I have a multiline TextView in Android. It is a child of a LinearLayout. It is already centered horizontally and I also want to have the text inside it centered.

Here's what I have now:

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

What I want is:

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

Where:

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

| = the TextView edge

Answer

Ovidiu Latcu picture Ovidiu Latcu · Jun 19, 2012

Did you try the gravity attribute on the TextView ?

  android:gravity="center"

And make sure to use android:gravity and not android:layout_gravity.