A. Ferrand
A. Ferrand

Reputation: 618

Wrong behavior of RTL parameters hack with android 4.2.2

Since android 4.2, RTL languages are fully supported.
Start and End can replace Left and Right to define layout but android 4.1 and older don't support start and end.
To use only one xml for both layout direction, we can use both start and left or end and right for retrocompatibility.
For exemple :

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    />

With android 4.3, device use only start and end parameters and ignore left and right where both are defined.

But with android 4.2.2 device use both parameters !

I have to use layout-v17 folder to duplicate each layout with start and end parameters.
There is an other solution ?

Upvotes: 8

Views: 1623

Answers (2)

Gillis Haasnoot
Gillis Haasnoot

Reputation: 2278

This post is old but still valid.

I have the same issue. I found out that this issue only occurs when the view is wrapped inside a relativelayout. In that situation a alignparentright in combination with alignparentend results in a full width view, because it uses both the end and right parameter. When you remove the relativelayout and replace it with a linearlayout only the alignparentend parameter cannot be used anymore, but you can use gravity. Android application itself makes sure that RTL ui gets mirrored

Upvotes: 1

Arthur
Arthur

Reputation: 1443

Well i don't know if there is any official work around, so i have implemented a hack.

I added this to the end of my gradle.build file

String targetRTLDir = "res/layout-ldrtl"
task copyTask(type: Copy) {
    delete fileTree(dir: targetRTLDir)

    from 'res/layout'
    into targetRTLDir

    filter { String line -> line.replaceAll('Right', 'RTL_R') }
    filter { String line -> line.replaceAll('Left', 'Right') }
    filter { String line -> line.replaceAll('RTL_R', 'Left') }
    filter { String line -> line.replaceAll('android:layout_gravity="right"', 'android:layout_gravity="rtl_r"') }
    filter { String line -> line.replaceAll('android:layout_gravity="left"', 'android:layout_gravity="right"') }
    filter { String line -> line.replaceAll('android:layout_gravity="rtl_r"', 'android:layout_gravity="left"') }
    filter { String line -> line.replaceAll('android:gravity="right"', 'android:gravity="rtl_r"') }
    filter { String line -> line.replaceAll('android:gravity="left"', 'android:gravity="right"') }
    filter { String line -> line.replaceAll('android:gravity="rtl_r"', 'android:gravity="left"') }
    filter { String line -> line.replaceAll('android:gravity="start"', 'android:gravity="right"') }
    filter { String line -> line.replaceAll('android:gravity="end"', 'android:gravity="left"') }
}

preBuild.dependsOn('copyTask')

What it does is simply copy all your files from layout to layout-ldrtl and swap all left and right.

You can also replace all left and right to start and end and place it into the layout-v17 folder if you want, but i've found that this works better since android:gravity="start" does not work!

I am fairly new to Gradle, so excuse me if this can be done with a much simpler script :)

Upvotes: 3

Related Questions