Saturday, October 31, 2015

Globalization & RTL Mirroring - Android

Developers working with global applications which need to support languages including Arabic, Farsi, Hebrew etc will have considerations and issues with them in mirroring layouts to support these languages.

Layout Mirroring is a term used to describe the ability of software to reorder the UI elements to match the right to left flow for that locale. In Android with API 17, automatic layout mirroring was introduced.

RTL For Right-To-Left (RTL) languages like Arabic, not only does the text alignment and text reading order go from right to left, but also the UI elements layout should follow this natural direction of going from right to left. Of course, this layout change would only apply to localized RTL languages. Mirroring is in fact nothing else than a coordinate transformation:


  • Origin (0,0) is in the upper RIGHT corner of a window
  • X scale factor = -1 (i.e. x values increase from right to left 









To use layout mirroring  in android flag this support in the <application> element in android manifest file.

android:supportsRtl="true"

All the layouts "left/right" properties should be changed to "start/end" equivalents. For example android:paddingLeft will become android paddingStart. As this is clear from the coordinate transformation part described above. For supporting older versions (earlier to 4.2 ) the code should have both android:paddingLeft and android:paddingStart properties defined.

 Android 4.2 includes set of new APIs to help manage View components for RTL and LTR support. 
·        android:layoutDirection —  direction of a component's layout.
·        android:textDirection —  direction of a component's text.
·        android:textAlignment — alignment of a component's text.
·        getLayoutDirectionFromLocale() — method for getting the Locale-specified direction


  HierarchyViewer now lets to see start/end properties, layout direction, text direction, and text alignment for all the Views in the hierarchy.

References :