Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is android bug in load "layout-ldltr" layout against "layout-ldrtl" after orientation changed?

In my project I have to create both layout-ldltr and layout-ldrtl layouts for Right-To-Left and Left-To-Right languages.

I launch application for Right-To-Left language, and every thing is good. But when orientation changed, android load layout-ldltr layout against layout-ldrtl, although current Locale is set to RTL language!!!

How can fix this problem?

In AndroidManifest.xml

<application
    android:name=".QuranApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

In layout-ldrtl\activity_main.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layoutDirection="rtl">

<include layout="@layout/toolbar" />

<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:layout_weight="1"
    android:layoutDirection="rtl">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutDirection="rtl"/>

    <include
        layout="@layout/list_view"
        android:layout_width="@dimen/drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>

UPDATE

After @JohanShogun comment, I change android:layoutDirection="rtl" to android:layoutDirection="locale"and problem solved for most items.

In first photo all element are shown very good:

Portrait orientation

In landscape mode, in StickyListHeader list view in header item that sticking at top of screen, Android used from ltr layout!:

Lanscape orientation

like image 971
Sayed Abolfazl Fatemi Avatar asked Jul 11 '15 13:07

Sayed Abolfazl Fatemi


1 Answers

If you have a folder with layout-land it will take president over layout-ldrtl etc. you may need to create folders for layout-land-ldrtl and so on.

An other way you can handle your right-to-left and left-to-right layouts is to keep one layout file which is written to work with both versions.

On a LinearLayout etc there is an attribute:

android:layoutDirection

You can set this to the value of "Locale" and your horizontal layouts will flip order.

Instead of using align_left/align_right and gravity_left/gravity_right use instead align_start/align_end and gravity_start/gravity_end (goes for all left/right attributes). The start and end tags depend on the layout direction. If the user has a locale with ltr start is left and end is right, if the user has a locale with rtl start is right and end is left.

I personally prefer using layout files that are written to work with both rtl and ltr over having separate ones, it's easy to miss an update in one version if you keep different files, resulting in a bad user experience.

Also move your layout to the layout folder (remove "-ldrtl") then change all android:layoutDirection="rtl" to android:layoutDirection="locale".

like image 61
JohanShogun Avatar answered Oct 10 '22 18:10

JohanShogun