Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set LayoutDirection (RTL) for api lower than 17

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> 
like image 387
hamidreza haajhoseini Avatar asked May 26 '15 16:05

hamidreza haajhoseini


1 Answers

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

like this :

ViewCompat.setLayoutDirection (View yourView, int layoutDirection) 

layoutDirection is a constant from ViewCompat class

like image 110
hamidrezabstn Avatar answered Sep 30 '22 17:09

hamidrezabstn