Skip to content
Fix Code Error

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

March 13, 2021 by Code Error
Posted By: J. Pablo Fernández

How do I center the text horizontally and vertically in a TextView, so that it appears exactly in the middle of the TextView in Android?

Solution

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);

Answered By: Bill

Related Articles

  • How to implement HorizontalScrollView like Gallery?
  • ListView inside ScrollView is not scrolling on Android
  • Android : Fragment doesn't fit into Fragment Container
  • Constraint Layout Vertical Align Center
  • MaterialCardView is in front of NavigationView. How can I…
  • Android adding simple animations while…
  • How to use ScrollView in Android?
  • Unable to run Robolectric and Espresso with a…
  • onClick for buttons on custom CardView
  • Android Layout Animations from bottom to top and top to…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Convert a PHP object to an associative array

Next Post:

Extract substring in Bash

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error