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

1

10

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

Montpelier answered 11/7, 2015 at 13:29 Comment(0)
N
6

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".

Nullipore answered 26/7, 2015 at 9:2 Comment(9)
Thank you for your reply. I added android:layoutDirection attribute, but not affect for this problem.Montpelier
if you use the android layout attribute you may want to skip your folders with rtl/ltr resources. Also please show your code with how you used the attributeNullipore
also remember to put this in your application manifest: android:supportsRtl="true" (goes on application level) developer.android.com/intl/es/guide/topics/manifest/…Nullipore
I edit question and add my code for your recommendations.Montpelier
Try this: Move your layout to the layout folder (remove "-ldrtl") then change all android:layoutDirection="rtl" to android:layoutDirection="locale"Nullipore
Very good, your comment solve problem for some layouts. please edit your answer and add this solution to it. I add screenshot to next answer to show result.Montpelier
Updated the answer, I'm not sure if you have more issues though? You may want to consider doing the same changes for your landscape layouts. The sticky header item may not have rtl support. If so you can probably add it quite easy by modifying it's resource files.Nullipore
I post an issue to github page of Sticky ListHeader plugin: github.com/emilsjolander/StickyListHeaders/issues/417 Thank you again for your helps and replies :)Montpelier
Alright :) if you think you can, consider attempting to fix the bug yourself and contributing it to the library. Either way, if this resolves all your non library related issues, please accept this answer. :)Nullipore

© 2022 - 2024 — McMap. All rights reserved.