If I just make a standard TextView
with the Justification
set to Center
, it looks something like this:
If I also want to center the text vertically, as well as horizontally, how do I do this?
android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered.
To center align the text in this Textview through layout file, set the android:textAlignment attribute for the TextView to “center”, as shown in the following activity_main. xml layout file.
We will start off with creating a custom TextView extending android's TextView class, call it VerticalTextView. The key to the vertical text view is the rotation. Inside the custom class, we define a constant 270f or 90f depending on how you want the text to be read which is the rotation angle.
You may be able to fudge such an alignment by dynamically updating pixels-above-lines
based on the size of the textview control and the size of the text being displayed in it.
TextViews are normally meant for a large amount of text that the user can edit. As such it may grow too large and have to scroll, hence the difficulty with vertical centering.
If you're only using it to display a message that doesn't have to be edited, you might try Gtk.Label
instead, which you can center vertically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With