<TextView
android:layout_height="30dp"
android:text="@string/list_title"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:textSize="@dimen/title_size"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:background="@color/white"
android:id="@+id/list_title"
/>
Is the first element in a RelativeLayout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
But the text in the TextView will not center. I cannot understand why as all gravities and the textAlignment
property are set to "center".
What am I doing wrong?
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. android:layout_centerInParent="true" if you want TextView in center position of parent view.
To center align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “center” in layout file, or programmatically set the textAlignment property of the TextView object with View. TEXT_ALIGNMENT_CENTER in activity file.
You can apply a centering to any View, including a Layout, by using the XML attribute android:layout_gravity". You probably want to give it the value "center".
To align text in TextView at the center vertically and horizontally we need to make use of gravity attribute. Example 1 : You can use center_vertical | center_horizontal value to set the text at the center of the TextView area.
Add android:gravity="center"
to your textView
Try this:
<TextView
android:layout_height="30dp"
android:text="@string/list_title"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:textSize="@dimen/title_size"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:background="@color/white"
android:id="@+id/list_title"
/>
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