While testing my layout on a device with RTL language (Arabic) I found that TextView with gravity:start keeps aligning the text to the left instead of right ! I tried android:textAlignment="viewStart" and it works correctly but due to API reqs I didn't depend on it.
my code (I mean the first textview in my code) :
<LinearLayout
android:orientation="horizontal"
android:gravity="center_vertical"
>
<TextView
android:text="Size"
android:gravity="start"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<LinearLayout android:gravity="center" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:text="000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="subtext"/>
</LinearLayout></LinearLayout>
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.
Left Align Text via Layout File 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.
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.
The android:gravity attribute is used to arrange the position of the content inside a view (for example text inside a Button widget).
For Full support or RTL you have to target api 17
If you are targeting your app to Android 4.2 (the app's targetSdkVersion or minSdkVersion is 17 or higher), then you should use “start” and “end” instead of “left” and “right”. For example
My current workaround for this if any one is interested in the future is adding an empty view between the two elements and making it fill the empty space between them (weight = 1) so they get aligned properly. Still I don't understand that abnormal behavior of gravity="start"
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