Android12 -> configuration.setLayoutDirection(new Locale("ar")) not working

409 Views Asked by At

It works on other android versions but below code doesn't work on android 12 devices. I use "ar" to language parameter.

val configuration: Configuration = resources.configuration
configuration.setLayoutDirection(Locale(language))
resources.updateConfiguration(configuration, resources.displayMetrics)
2

There are 2 best solutions below

1
段浩浩 On

I had the same problem. Maybe a cache to recreate life cycle configuration, I use this:

val intent = activity.intent
activity.finish()
startActivity(intent)

It can take effect.

0
Angel S On

I had the same problem and the add this piece of code and now it works. I don't know if it's the efficient way or not.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        (context as Activity).window.decorView.layoutDirection = 
            if (Locale.getDefault().language.equals("fa"))
                  View.LAYOUT_DIRECTION_RTL else View.LAYOUT_DIRECTION_LTR
}