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

halfer
  • 19,824
  • 17
  • 99
  • 186
Sayed Abolfazl Fatemi
  • 3,678
  • 3
  • 36
  • 48

1 Answers1

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

Sayed Abolfazl Fatemi
  • 3,678
  • 3
  • 36
  • 48
JohanShogun
  • 2,956
  • 21
  • 30
  • Thank you for your reply. I added android:layoutDirection attribute, but not affect for this problem. – Sayed Abolfazl Fatemi Jul 26 '15 at 10:01
  • 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 attribute – JohanShogun Jul 26 '15 at 10:55
  • also remember to put this in your application manifest: android:supportsRtl="true" (goes on application level) http://developer.android.com/intl/es/guide/topics/manifest/application-element.html – JohanShogun Jul 26 '15 at 10:56
  • I edit question and add my code for your recommendations. – Sayed Abolfazl Fatemi Jul 26 '15 at 11:49
  • 1
    Try this: Move your layout to the layout folder (remove "-ldrtl") then change all android:layoutDirection="rtl" to android:layoutDirection="locale" – JohanShogun Jul 26 '15 at 11:58
  • 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. – Sayed Abolfazl Fatemi Jul 26 '15 at 13:28
  • 1
    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. – JohanShogun Jul 26 '15 at 14:30
  • I post an issue to github page of Sticky ListHeader plugin: https://github.com/emilsjolander/StickyListHeaders/issues/417 Thank you again for your helps and replies :) – Sayed Abolfazl Fatemi Jul 26 '15 at 14:48
  • 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. :) – JohanShogun Jul 26 '15 at 15:39