Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android align text top center

I'm sorry if this is a duplicate but i couldn't find anything that was quite what I'm looking for. Basically, i want to align the text in the text View to be in the top center. And I would like to do this in XML if possible. So i want to combine

android:gravity="center"

and

android:gravity="top"

so that it aligns it with both. I have already tried to put both attributes in the text View element but it just gives an error.

like image 560
user3810543 Avatar asked Jul 10 '14 18:07

user3810543


People also ask

How do you center text on android?

To center align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “center” in layout file, or programmatically set the textAlignment property of the TextView object with View.

How do you center align text?

Select the text that you want to center. in the Page Setup group, and then click the Layout tab. In the Vertical alignment box, click Center. In the Apply to box, click Selected text, and then click OK.

How do I center text in a text box in android Studio?

Try this: Using LinearLayout if you are using textview height and width match_parent You can set the gravity text to center and text alignment to center text horizontal if you are using textview height and width wrap_content then add gravity center attribute in your LinearLayout. Save this answer.


2 Answers

First, you cannot duplicate the attribute, though you can combine values.

android:gravity="g1|g2"

However, center implies both vertical and horizontal centering so top|center wouldn't be correct. You should use

android:gravity="top|center_horizontal"
like image 165
matiash Avatar answered Oct 25 '22 17:10

matiash


If you want to apply more than one propery to gravity you must use | like this

android:gravity="top|center"
like image 29
Johan Lindkvist Avatar answered Oct 25 '22 17:10

Johan Lindkvist