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

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

Im assuming youre using XML layout.

<TextView  
    android_layout_width=match_parent 
    android_layout_height=match_parent 
    android_gravity=center
    [email protected]/**yourtextstring**
/>

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

As @stealthcopter commented, in java: .setGravity(Gravity.CENTER);.

And for Kotlin users, .gravity = Gravity.CENTER

android:gravity=center 

This will do the trick

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

You can also set it up dynamically using:

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

Leave a Reply

Your email address will not be published.