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

9

43

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!

Dysuria answered 24/10, 2014 at 13:55 Comment(0)
C
70

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.

Cabezon answered 24/10, 2014 at 14:58 Comment(9)
Thanks. I normally use this "technique" to determine wether the app is running on a tablet device or not ;)Cabezon
I ended up using this over my solution because the above works all the time, my solution doesnt work inside onCreateViewDysuria
Glad I could help you out ;)Cabezon
Might want to add a "?>" to end of first line on those xml files, just for correct syntax.Brest
Thanks. Just added them... ;)Cabezon
You don't need extra resources for this. Check out my answer here : https://mcmap.net/q/377909/-android-determine-if-device-is-in-right-to-left-language-layoutElonore
I added this line in my strings resource files. no need to create special file for itBerner
The only working answerAmphetamine
This will crash on old android devices.Carrol
E
45

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)

Elonore answered 5/11, 2017 at 9:14 Comment(3)
I just tested this in Android 10 with the device language set to Arabic, which my app is not translated into, and Locale.getDefault() returned en_US with LTR layout direction. Maybe this method gets the app's locale's layout direction instead of the device's?Sequential
@Sequential In that case it's correct. If the app is in English, you want to treat it as LTR without respect to device's language.Gullett
@Sequential Locale.getDefault() is supposed to return the current OS locale. Maybe you didn't set it right, or you didn't call the function again with the updated result of Locale.getDefault(). The code works with what it has. If it says it's English, it's LTR.Elonore
D
28

Gr, a little bit longer googling and I would have found it before posting:

if (ViewCompat.getLayoutDirection(getView()) == ViewCompat.LAYOUT_DIRECTION_LTR) {
    // ...
}
else {
    // ...
}

http://developer.android.com/reference/android/support/v4/view/ViewCompat.html#getLayoutDirection(android.view.View)

Dysuria answered 24/10, 2014 at 14:14 Comment(1)
You don't need a view to get it. Check out my answer here: https://mcmap.net/q/377909/-android-determine-if-device-is-in-right-to-left-language-layoutElonore
H
11

If you are using Core KTX

you could use:

if (Locale.getDefault().layoutDirection == LayoutDirection.RTL)
Helicograph answered 20/9, 2020 at 16:34 Comment(2)
if api 19 is finePrecautionary
Ah layoutDirection seems to be an extension property? Didn't know those existed. It maps to TextUtils.getLayoutDirectionFromLocale() which is equivalent to the answer of @androiddeveloper.Trilbi
K
10

Kotlin Code for detect app layout direction :

 val config = resources.configuration
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        if (config.layoutDirection == View.LAYOUT_DIRECTION_LTR) {
           // showAlertMsg("LTR")
        } else {
           // showAlertMsg( "RTL")
        }
    }
    else {
        //TODO("VERSION.SDK_INT < JELLY_BEAN_MR1")
           }
Kaohsiung answered 5/1, 2019 at 9:8 Comment(0)
E
5

If this is still getting considerable traffic, people should also consider Configuration.getLayoutDirection(), for API 17+. It will return either LayoutDirection.LTR or LayoutDirection.RTL. I imagine ViewCompat.getLayoutDirection(android.view.View) returns the direction that was actually used to layout a particular view. If you're doing something weird, such as trying to determine which person should be on which side of a chat screen (like me), this might be of use.

Effectually answered 2/4, 2017 at 5:15 Comment(3)
There is a way for it to work on all API versions, without a view, here: https://mcmap.net/q/377909/-android-determine-if-device-is-in-right-to-left-language-layoutElonore
I think it's good to use ViewCompat.getLayoutDirection(android.view.View) because it's common to hardcode direction of some views to not rotate in RTL languages or vice versa.Incunabula
The poster asked the following: "Is there a way to determine if the device is in a right to left language". My answer is that there is a method in Configuration that does exactly this. These methods that have been suggested in View and ViewCompat, check only for a specific View, not the device.Effectually
L
2

That's so easy. Just use: isRtll()

override fun isRtl(): Boolean {
    return super.isRtl()
}

Have a fun!

Loquitur answered 29/6, 2021 at 6:22 Comment(0)
C
1

If your Build.VERSION.SDK_INT is bigger than JELLY_BEAN (16) you can use this:

val locale = Locale.getDefault() 
val isLtr = TextUtils.getLayoutDirectionFromLocale(locale) == ViewCompat.LAYOUT_DIRECTION_LTR
Clavicle answered 25/11, 2020 at 9:8 Comment(0)
L
0

Yes,use the Bidi.getBaseLevel() function, it will return 0 for left-to-right and 1 for right to left.

http://developer.android.com/reference/java/text/Bidi.html#getBaseLevel()

Laurellaurella answered 24/10, 2014 at 14:8 Comment(4)
I would have to have an instance of Bidi correct? In any case, I found a solution that seems to work. thanks though!Dysuria
Yes, you have to istantiate one.Laurellaurella
Please post the solution you found, for future reference. Thanks.Laurellaurella
You don't have to create a new instance of anything. You can use this instead: https://mcmap.net/q/377909/-android-determine-if-device-is-in-right-to-left-language-layoutElonore

© 2022 - 2024 — McMap. All rights reserved.