Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android determine if device is in right to left language/layout

Is there a way to determine if the device is in a right to left language (something like Arabic) as opposed to something that's left to right (English)?

Something compatible with older API levels (down to 10) is necessary

SOLUTION

i ended up using the xml method in the accepted answer. Farther down the line, i also added the code indicated here for instances where I didn't have access to getResources()

Identifying RTL language in Android

more info

This question still gets a lot of traffic; something else I wanted to point out: When I originally asked this I think it was partially to help address showing different pointing chevrons in RTL vs LTR -- another slick way of accomplishing this is by placing drawable resources in the standard and ldrtl directories -- means no code required to determine which one to show!

like image 419
joshkendrick Avatar asked Oct 24 '14 13:10

joshkendrick


People also ask

What is RTL in android?

android.util.LayoutDirection. A class for defining layout directions. A layout direction can be left-to-right (LTR) or right-to-left (RTL). It can also be inherited (from a parent) or deduced from the default language script of a locale.

How do I enable RTL on android?

To force right-to-left layouts on your Android device you need to open Settings, and access the Developer options menu (if developer options aren't enabled, here's how to do it). Once you've accessed the menu, scroll down to find the “Force RTL layout direction” tab. Tap on it to activate the option, and you're done.

How do I change RTL on android?

Just go to Android Studio > Refactor > Add RTL support where possible… I would recommend you checking your app once after applying this change as you might not want all your Layouts/Views to be RTL. If you want to force any layout to LTR then just add android:layoutDirection="ltr" to that view.


2 Answers

You could create a values-ldrtl folder with a xml file called isrighttoleft.xml:

<?xml version="1.0" encoding="utf-8"?> <resources>     <bool name="is_right_to_left">true</bool> </resources> 

and in your values folder the same file with:

<?xml version="1.0" encoding="utf-8"?> <resources>     <bool name="is_right_to_left">false</bool> </resources> 

And finally in Code:

boolean isRightToLeft = getResources().getBoolean(R.bool.is_right_to_left); 

The values-ldrtl will only be used on a device where the specific settings (e. g. Language) are right-to-left-read languages.

like image 136
ezcoding Avatar answered Sep 22 '22 18:09

ezcoding


I think this is a better way:

val isLeftToRight = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_LTR 

Docs:

https://developer.android.com/reference/android/support/v4/text/TextUtilsCompat.html#getLayoutDirectionFromLocale(java.util.Locale)

Or, if you have minAPI 17 or above, you can just use this:

val isLeftToRight=TextUtils.getLayoutDirectionFromLocale(Locale.getDefault())==View.LAYOUT_DIRECTION_LTR 

Docs:

https://developer.android.com/reference/android/text/TextUtils.html#getLayoutDirectionFromLocale(java.util.Locale)

like image 31
android developer Avatar answered Sep 18 '22 18:09

android developer