Android now offers several APIs that allow you to build user interfaces that gracefully transform layout orientation to support languages that use right-to-left (RTL) UIs and reading direction, such as Arabic and Hebrew.
To begin supporting RTL layouts in your app, set the android:supportsRtl
attribute to the <application>
element in your manifest file and set it “true"
. Once you enable this, the system will enable various RTL APIs to display your app with RTL layouts. For instance, the action bar will show the icon and title on the right side and action buttons on the left, and any layouts you’ve created with the framework-provided View
classes will also be reversed.
If you need to further optimize the appearance of your app when displayed with an RTL layout, there are two basic levels of optimization:
- Convert left- and right-oriented layout properties to start- and end-oriented layout properties.
For example, use
android:layout_marginStart
in place ofandroid:layout_marginLeft
andandroid:layout_marginEnd
in place ofandroid:layout_marginRight
.The
RelativeLayout
class also provides the corresponding layout attributes to replace left/right positions, such asandroid:layout_alignParentStart
to replaceandroid:layout_alignParentLeft
andandroid:layout_toStartOf
instead ofandroid:layout_toLeftOf
. - Or to provide complete optimization for RTL layouts, you can provide entirely separate layout files using the
ldrtl
resource qualifier (ldrtl
stands for layout-direction-right-to-left}). For example, you can save your default layout files inres/layout/
and your RTL optimized layouts inres/layout-ldrtl/
.The
ldrtl
qualifier is great for drawable resources, so that you can provide graphics that are oriented in the direction corresponding to the reading direction.
Various other APIs are available across the framework to support RTL layouts, such as in the View
class so that you can implement the proper behaviors for custom views and in Configuration
to query the current layout direction.
Note: If you are using SQlite and have tables or column names that are “number only," be careful: usingString.format(String, Object...)
can lead to errors where the numbers have been converted to their Arabic equivalents if your device has been set to the Arabic locale. You must use String.format(Locale,String,Object...)
to ensure numbers are preserved as ASCII. Also use String.format("%d", int)
instead of using String.valueOf(int)
for formatting numbers.