Reputation:
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
Upvotes: 66
Views: 96226
Reputation: 1815
If you just want to align your text in textView then textAlignment
is better option then gravity.
Add android:textAlignment="center"
for Center
Add android:textAlignment="textStart"
for Left
Add android:textAlignment="textEnd"
for Right
Upvotes: 1
Reputation: 536
The gravity attribute is the update to Alignment. Gravity is available in the attributes inspector as well as being able to do;
android:gravity="center"
Upvotes: 3
Reputation: 131
Make the text (which you want to set into TextView) in strings.xml as following :
<string name="content">
<center>Your text goes here.</center>
</string>
and in TextView add following attribute:
<TextView
...
android:text="@string/content"/>
It will center the text into TextView horizontally.
Upvotes: -1
Reputation: 31
Yes, use android:gravity attribute to change the position of data inside views.
Upvotes: 3
Reputation: 974
android:gravity="center_horizontal"
should do the trick.
More information could be found here or here.
Upvotes: 15
Reputation: 72311
Did you try the gravity
attribute on the TextView
?
android:gravity="center"
And make sure to use android:gravity
and not android:layout_gravity
.
Upvotes: 173