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 ?
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With