Wednesday, 28 June 2017

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




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?


Answer



I'm assuming you're using XML layout.



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


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...