To right align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “viewEnd” in layout file, or programmatically set the textAlignment property of the TextView object with View.
Adding android:gravity="center" in your TextView will do the trick (be the parent layout is Relative/Linear )!
android:layout_centerHorizontal="true" android:gravity="center" I think it's correct. so that TextView will be centered in Layout, and text will be centered in TextView.
To left align the text in this Textview through layout file, set the android:textAlignment attribute for the TextView to “viewStart”, as shown in the following activity_main. xml layout file.
Did you try the gravity
attribute on the TextView
?
android:gravity="center"
And make sure to use android:gravity
and not android:layout_gravity
.
android:gravity="center_horizontal"
should do the trick.
More information could be found here or here.
Yes, use android:gravity attribute to change the position of data inside views.
The gravity attribute is the update to Alignment. Gravity is available in the attributes inspector as well as being able to do;
android:gravity="center"
If you just want to align your text in textView then textAlignment
is better option then gravity.
Add android:textAlignment="center"
for Center
Add android:textAlignment="textStart"
for Left
Add android:textAlignment="textEnd"
for Right
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