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>
  • 2
    Not sure there is a really good answer. For layouts I have tended to use locale or ldrtl folders. See also http://stackoverflow.com/questions/15746091/how-to-handle-rtl-languages-on-pre-4-2-versions-of-android – QuantumTiger Nov 26 '15 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 Dec 06 '15 at 07:20
  • Possible duplicate of [Android below 4.2 how to set layoutDirection to be RTL](http://stackoverflow.com/questions/20377219/android-below-4-2-how-to-set-layoutdirection-to-be-rtl) – PEHLAJ May 25 '16 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? – hamidreza haajhoseini May 25 '16 at 14:21

2 Answers2

19

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

like this :

ViewCompat.setLayoutDirection (View yourView, int layoutDirection)

layoutDirection is a constant from ViewCompat class

Community
  • 1
  • 1
hamidrezabstn
  • 493
  • 7
  • 17
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);