30

I am going to build a Persian app (RTL). my app includes a ListView and a Navigation Drawer .

I added in Manifest in application tag android:supportsRtl="true"

and in onCreate() method: getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

I know that LayoutDirection for less than 17 api does not work.

for api level 17 and higher:

enter image description here

for api lower than 17:

enter image description here

How do I solve this problem. 1- Putting a conditional statement to check the android api and assign a particular layout ? 2-use folder layout-ldrtl in eclipse? 3 - ... ? What is the best way??

<android.support.v4.widget.DrawerLayout

       xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/drawer_layout"


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/myback">



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="40dp"  >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp" >




        <EditText
        android:id="@+id/edtxtsearch"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
         android:hint="عبارت مورد جستجو"
          android:textSize="13dp"
         >

        <requestFocus />
    </EditText> 


        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_gravity="center"

            >


            <ListView
                        android:id="@+id/mylist"
                        android:layout_width="200dp"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center_horizontal"
                      android:divider="@android:color/transparent"
                      android:dividerHeight="7dp"
                      android:scrollbarStyle="outsideInset"
                      />



           </LinearLayout>



</LinearLayout>




<FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

         >



    </FrameLayout>



     <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:background="#ffffff"
        android:dividerHeight="4dp"  
         android:divider="@android:color/transparent"     

       />

    </android.support.v4.widget.DrawerLayout>
4
  • 2
    Not sure there is a really good answer. For layouts I have tended to use locale or ldrtl folders. See also stackoverflow.com/questions/15746091/… Commented Nov 26, 2015 at 20:39
  • @QuantumTiger pretty much nailed it. I will only add that prior 4.2 some vendors (device manufacturers and mobile operators) sometimes provided their custom solutions for RTL layouts and bi-di text. This helped them at the time to push their products to ME markets, and enabled some pre-installed apps to deliver reasonable UX. But from today's point of view, this adds to fragmentation: be prepared to find that your app tuned for AOSP 4.2 will behave strangely on Samsung Galaxy S2 (ME ROM) or others.
    – Alex Cohn
    Commented Dec 6, 2015 at 7:20
  • Possible duplicate of Android below 4.2 how to set layoutDirection to be RTL
    – PEHLAJ
    Commented May 25, 2016 at 11:07
  • @P.Rai I know that layoutDirection not supported for Android less than 4.2.I've said this in the context of question . I mean that is there another way to set right alignment Navigation Drawer? Commented May 25, 2016 at 14:21

2 Answers 2

19

use ViewCompat from android.support.v4.view.ViewCompat !

like this :

ViewCompat.setLayoutDirection (View yourView, int layoutDirection)

layoutDirection is a constant from ViewCompat class

6
  • 1
    Thanks, but this code works on Android 4.2 and higher Commented Jun 8, 2016 at 14:21
  • Are u sure ? I have used support library for it ! Commented Jun 8, 2016 at 16:27
  • LayoutDirection is not defined on api level 16 and below so Drawelayout in my app also will be on the left of screen. Commented Jun 9, 2016 at 5:38
  • It's doesn't work at all. Check the sources - any code is executed only if API >= 17. I am really wondering how this answer can have co many pluses, as in the topic is information about API lower than 17.
    – Cililing
    Commented May 17, 2019 at 9:18
  • 2
    Not Working for API below 17 :| Commented Jun 18, 2019 at 12:26
0

Just concatenate every string that you want to set in textView with "\u202B"

it just enforces the text to be RTL.

String textWhichIsLTR = "LTR or RTL text that you want to enforce to RTL";
String enforcedRTL = textWhichIsLTR + "\u202B";
fooTextView.setText(enforcedRTL);

Not the answer you're looking for? Browse other questions tagged or ask your own question.