Android: gravity = "right" does not work on all devices for Arabic and other RTL languages

I have an Android app in Arabic and here is the XML code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/greygradientbackground"> <ImageView android:id="@+id/logo" android:layout_width="150dp" android:layout_height="fill_parent" android:scaleType="centerCrop" android:layout_margin="5dp" android:layout_alignParentTop="true" android:layout_alignParentRight="true" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="#000000" android:layout_gravity="center_vertical|right" android:gravity="right" android:layout_toLeftOf="@id/logo"/> </RelativeLayout> 

The problem is that android: gravity works on some Android models, but not on others.

To be more specific, I tested the application on many 2.3.3 Android devices, and the Arabic text is aligned to the right. However, on other 2.3.3 devices, Arabic text is aligned to the left (which is incorrect).

When I changed android: gravity = "right" to android: gravity = "left" the problem moved from the second group of devices to the first.

So my question is, how can I solve this problem, especially since, as far as I know, there is no way to localize layouts based on the model number of the device.

Thanks in advance for any guidance because I totally lost. :(

UPDATE:

I searched about "How to properly align the Arabic language on all versions of Android?" but nothing works on all my test devices. Any suggestions please? I'm sure there is an optimal approach for aligning Arabic text on Android.

UPDATE 2:

I tried using WebView instead of TextView to properly align the Arabic language using CSS styles. However, Arabic text appears as strange characters in WebView.

Here is the code:

 mWebView.loadData("<html dir=\"rtl\">Ψ§Ω„Ψ£Ψ³Ω…<body></body></html>", "text/html", "UTF-8"); 

It is strange that the text of the Arabic websites is displayed correctly. So what is the problem ?: (

+7
source share
4 answers

This is the expected behavior. Instead, you can use gravity="right" , but gravity="end" , the same idea that you apply to gravity="left" , which you can use gravity="start" , as well as layout_marginStart instead of layout_marginLeft

Thus, Android will place the text in the initial orientation depending on the location. (for us Americans, start writing on the left and end on the right , but run the Arabs from the right and the end on the left ).

You can get the layout / text using the getLayoutDirectionFromLocale(null) method, this will return View.LAYOUT_DIRECTION_LTR (from left to right) or View.LAYOUT_DIRECTION_RTL (from right to left, like Arabic).

You can check more details here.

+4
source

change your text in the layout to this ....

 <TextView android:id="@+id/title" 

change layout_width to "fill_parent"

  android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="#000000" android:layout_gravity="center_vertical|right" android:gravity="right" android:layout_toLeftOf="@id/logo" /> 

You can change it differently as well .... You can make changes to the activity class .. it should ... let your TextView with the identifier name appear in the activity class with the TextView object named "tv" "you can do it right by writing this.

  tv.setGravity(Gravity.RIGHT); 

but in both cases the width of the breadboard layout should be "fill_parent". If you don’t do this and save "wrap_content", then there is no place to move the text ... all alignments (in the center, left, right ...) remain the same .....

If both of these participants work, then I would prefer that you use a table layout and keep a text view in it ....

0
source

try putting the text in a separate LinearLayout and set the layout properties: android: layout_gravity = "center_vertical" and android: orientation = "vertical" Android: gravity = "right"

note: do not specify layout_gravity text.

hope this helps! it worked for me.

You can also try the solution presented here: Animated text in Android Arabic text

0
source

Another solution to achieve this behavior:

 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layoutDirection="ltr" android:layout_weight="1" android:gravity="end" android:padding="16dp" android:text="@string/locale_switch_btn" /> </LinearLayout> 

Note that in order to properly align gravity = "end" in rtl locales you must set the property:

Android: LayoutDirection = "l"

0
source

All Articles