I have a multilingual app with primary language English and secondary language Arabic.
I am calling setLocale()
in the onCreate()
of every Activity
in my app:
public static void setLocale(Locale locale){
Locale.setDefault(locale);
Context context = MyApplication.getInstance();
final Resources resources = context.getResources();
final Configuration config = resources.getConfiguration();
config.setLocale(locale);
context.getResources().updateConfiguration(config,
resources.getDisplayMetrics());
}
where locale
is one of the following:
The above method is called before super.onCreate(savedInstanceState)
gets called.
As described in the documentation,
- I have added
android:supportsRtl="true"
in the manifest. - I have changed all xml properties with
left
andright
attributes tostart
andend
respectively. - I have put Arabic-language strings in
res\values-ar\strings
folder and drawable resources inres\drawable-ar
folder (and similarly for other resources).
The above setup works properly. After changing the Locale
to ar-AE
, Arabic text & resources are correctly displayed in my Activities.
However, there is a problem with both resources and layout direction for all Android devices with version 8.0 and above.
On a device with version less than 8.0, an RTL screen correctly looks like this:
And on all devices with 8.0+, the same screen turns up looking like this:
which is wrong.
It turns out that both the direction and the resources are getting displayed incorrectly.
There are two problems here:
- The correct
Locale
does not seem to be updated across the app configuration. - The direction of the text and drawables is opposite of what it should be.
With respect to direction, a curious method called setLayoutDirection()
exists which I had not noticed before.
I would like to know what this problem is, why it happens in Oreo and what is the solution for it. Please help / comment on this.
EDIT:
According to the API Differences report, the
updateConfiguration()
method was indeed deprecated in Android 7.1 (API level 25).
Also, found all the relevant posts on this. In order of importance:
1. Android N change language programmatically.
2. Android context.getResources.updateConfiguration() deprecated.
3. How to change Android O / Oreo / api 26 app language.
4. Android RTL issue in API 24 and higher on locale change
5. Change language programmatically (Android N 7.0 - API 24).