I want to change the language of the app and this works fine until API 26.
For api > 25 I put Locale.setDefault(Locale.Category.DISPLAY, mynewlanglocale);
It is possible, however i would not recommend to set the language programatically
Android is designed so the System UI and your App have the same language, if you change it programmatically you would be fighting the system
Instead what you can do is enable multilanguage support by adding different strings.xml languages, this will change the language automatically
I reccommend reading through this Google Developers article:
Supporting Different Languages and Cultures
If you really need to change it programatically you can do the following
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
On SDK >= 21, you need to call 'Resources.updateConfiguration()', otherwise resources will not be updated.
Hope it helps.
After use all solution in all sources finally i found my issue. That makes me angry for 2 days.
Everyone knows that in Android Oreo (API 26) we must use createConfigurationContext
, But My problem is using Country name with local.
Replace
en_US with en
ar_AE with ar
fa_IR with fa
And my problem solved.
Hope to help someone