How to center a two-line text in a TextView on Android? How to center a two-line text in a TextView on Android? android android

How to center a two-line text in a TextView on Android?


If you just want to center it (I'm assuming the \n is working to split the lines), just add android:gravity="center_horizontal" rather than layout_gravity.
Using layout gravity moves the actual TextView, using gravity affects the content of the TextView.


Had to add both gravity and layout_gravity to align both TextView and its content

android:gravity="center_horizontal"android:layout_gravity="center_horizontal"


if your width is wrap_content, you have to set gravity and layout_gravity both as "center_horizontal"

<TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="two\nlines"    android:layout_gravity="center_horizontal"    android:gravity="center_horizontal"/>

However, if your width is "match_parent, you will only have to set gravity as "center_horizontal"

<TextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="two\nlines"    android:gravity="center_horizontal"/>

Hope it helps!