Change Language Programmatically in Android
Here is the appropriate way of changing the locale of the application:
There are some difficulties which you have to overcome to change the language programmatically.
1.)Your application will not remember your language change after it is closed or recreated during the configuration change.
2.)You should update currently visible UI properly according to the selected language.
Solution
“LocaleHelper
” is the solution all you need. You just have to initialize locale on your application’s main class. After that all your language changes will persist.
After the recent changes in Android API Version 24(Nougat) we need to override attachBaseContext to reflect changes.
Below method used to change language for application :
private static boolean updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return true;
}
find more details in below link :
Changing Android’s Locale Programmatically
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…