How do I center text horizontally and vertically in a TextView? How do I center text horizontally and vertically in a TextView? android android

How do I center text horizontally and vertically in a TextView?


I'm assuming you're using XML layout.

<TextView      android:layout_width="match_parent"     android:layout_height="match_parent"     android:gravity="center"    android:text="@string/**yourtextstring**"/>

You can also use gravity center_vertical or center_horizontal according to your need.

and as @stealthcopter commented in java: .setGravity(Gravity.CENTER);


android:gravity="center" 

This will do the trick


You can also set it up dynamically using:

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);