Android: center text horizontally and vertically in a TextView

By default, text appears on the top left of a TextView.

android-centrer-textview

To center it vertically, you need to use the gravity property :

android-centrer-textview-vertically

In your xml layout file, add the following property :

android:gravity="center_vertical"

To center the text vertically and horizontally :

android-centrer-textview-vertically-horizontally

Use the following property :

android:gravity="center"

To do it programmatically, use the following syntax :

MyTextView.setGravity(Gravity.CENTER)