Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:gravity="start" in RTL aligns the text to left not right

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>
like image 704
Bialy Avatar asked May 03 '16 05:05

Bialy


People also ask

How do you right align text on android?

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.

How do you left align text on android?

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.

How do I align text in text view?

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.

What is gravity start android?

The android:gravity attribute is used to arrange the position of the content inside a view (for example text inside a Button widget).


2 Answers

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

Native RTL support in Android 4.2

like image 151
N J Avatar answered Nov 10 '22 01:11

N J


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"

like image 37
Bialy Avatar answered Nov 09 '22 23:11

Bialy