I have an app, where the app language is slovak if the user's device language is set to Slovak, otherwise it will be EN. So the language is not changing in app, but is from instaliing SK or EN.
String languageToLoad = Resources.getSystem().getConfiguration().locale.getLanguage();
String lng = "sk".equals(languageToLoad) ? "sk" : "en";
Locale locale = new Locale(lng);Locale.setDefault(locale);
Configuration config = new Configuration();config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
I noticed, that locale in Configuration has been deprecated and also updateConfiguration(Configuration, DisplayMetrics) in Resources has been deprecated from android N (I think).
However for some reason it still works also for higher Android versions, but Android studio says it is deprecated.
So I tried to separate the code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String languageToLoad = Resources.getSystem().getConfiguration().getLocales().get(0).getLanguage();
String lng = "sk".equals(languageToLoad) ? "sk" : "en";
Locale locale = new Locale(lng);Locale.setDefault(locale);
Configuration config = new Configuration();config.setLocale(locale);
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
} else {
String languageToLoad = Resources.getSystem().getConfiguration().locale.getLanguage();
String lng = "sk".equals(languageToLoad) ? "sk" : "en";
Locale locale = new Locale(lng);Locale.setDefault(locale);
Configuration config = new Configuration();config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
but the first condition (for >=N) is not ok, I am not sure how to replace the current code. It still says the updateConfiguration is deprecated.
Even if I try getBaseContext().createConfigurationContext(config);
it doesn't change the language