9

I've got an Arabic Android application, 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 and on others don't.

To be more specific, I've tested the app on many 2.3.3 android devices and the Arabic text is aligned right. However on other 2.3.3 devices the Arabic text is aligned left (which is wrong).

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

So my question is how can I solve this issue especially that as far as I know there aren't a way to localize layouts based on a device model number.

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

UPDATE:

I searched about "How to align Arabic correctly on all Android versions?" but found nothing working on all my testing devices. Any suggestions please? I am sure there is a best practice approach for aligning Arabic text on Android.

UPDATE 2:

I tried to use a WebView instead of a TextView to align Arabic correctly using CSS styles. However, the Arabic text is showing in the WebView as strange characters.

Here is the Code:

mWebView.loadData("<html  dir=\"rtl\">الأسم<body></body></html>", "text/html", "UTF-8");

The strange thing is that Arabic websites text is displayed correctly. So, what's the problem? :(

j0k
  • 22,600
  • 28
  • 79
  • 90
M.ES
  • 920
  • 14
  • 30
  • Hello, I too have this issue in 3.x and above verions also.So can any one suggest some answer for this. – Raj Apr 18 '12 at 07:48

4 Answers4

5

This is an expected behaviour. You may not use gravity="right", but gravity="end" instead, the same idea you apply to gravity="left" which you may use gravity="start", as well as layout_marginStart instead of layout_marginLeft

This way, Android will put the text to the "start" orientation, depending on the location. (for us, americans, we start to write from the left and we end on the right, but arabians start from the right and end on the left).

You can get the layout/text directions with the method getLayoutDirectionFromLocale(null), this will return View.LAYOUT_DIRECTION_LTR (left-to-right) or View.LAYOUT_DIRECTION_RTL (right-to-left, like arabic).

More info you can check here

Murillo Ferreira
  • 1,423
  • 1
  • 16
  • 31
0

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

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

change your 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 the thing in another way also....You can make changes in the activity class..as follows...let your TextView with id title is mapped in the activity class with a TextView object named "tv" the you can make it right aligned by writing this.

 tv.setGravity(Gravity.RIGHT);

but in both the cases the width layout width should be "fill_parent".. If you do not do this and keep it "wrap_content" then there is no space for the text to move... all the alignments (center,left,right...)remain the same.....

If both of these donot work out... then I would prefer you to use table layout and keep the text view in it....

sagar
  • 151
  • 2
  • 5
  • 14
  • Hi Sagar,Thanks for your replay. I've already tried this and its not working in android 3.1 tablets. if i give the gravity as Right its coming on the left side. and if i give gravity Left, its coming on the right side. but other devices with OS v4.o and all are working fine. – Raj Apr 25 '12 at 10:02
  • @sagar Thanks for your help. Unfortunately, it didn't work. :( – M.ES Apr 26 '12 at 08:23
0

try putting your textview inside a separate LinearLayout ,and set that layouts properties likethis: android:layout_gravity="center_vertical" and android:orientation="vertical" android:gravity="right"

note: dont specify layout_gravity of the textview.

hope this helps! it worked fine for me.

also you can try the solution provided here: Android Arabic text aligment

Community
  • 1
  • 1
user173488
  • 947
  • 1
  • 9
  • 20
0

Another solution to achieve such 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, for correct gravity="end" alignment, in rtl locales you should set property:

android:layoutDirection="ltr"

Fragment
  • 1,555
  • 1
  • 26
  • 33