Set LayoutDirection (RTL) for api lower than 17
Asked Answered
S

2

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>
Secondrate answered 26/5, 2015 at 16:4 Comment(4)
Not sure there is a really good answer. For layouts I have tended to use locale or ldrtl folders. See also #15746591Terrance
@Terrance 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.Tersanctus
Possible duplicate of Android below 4.2 how to set layoutDirection to be RTLHendecahedron
@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?Secondrate
D
19

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

like this :

ViewCompat.setLayoutDirection (View yourView, int layoutDirection)

layoutDirection is a constant from ViewCompat class

Ditch answered 7/6, 2016 at 5:0 Comment(6)
Thanks, but this code works on Android 4.2 and higherSecondrate
Are u sure ? I have used support library for it !Ditch
LayoutDirection is not defined on api level 16 and below so Drawelayout in my app also will be on the left of screen.Secondrate
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.Telegonus
Not Working for API below 17 :|Overmatter
Salam, It really helps, ThanksDagley
N
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);
Nave answered 13/2, 2020 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.