I can perfectly change the language into my app and it works with every activity. Never had any problems (with my S4, and my friend's phones). But when I do it with my S9 (Oreo 8.0) nothing works. The language changes for some things but the most part of my app doesn't change the language. I don't understand, what changed with oreo that doesn't work anymore with what I did?
Here is what I do to change it (the example is for spanish language):
private View.OnClickListener btnLangue3Listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
langue.setBackground(ResourcesCompat.getDrawable(getResources(), R.mipmap.spanishflag, null));
langueMenu.setVisibility(View.GONE);
if (Locale.getDefault().getLanguage().equals("fr") || Locale.getDefault().getLanguage().equals("en")) {
Context context = getApplicationContext();
changeLang(context ,"es");
saveLocale("es");
restartActivity();
}
}
};
private void restartActivity() {
Intent mStartActivity = new Intent(getApplicationContext(), SplashActivity.class);
finish();
CharSequence text = getApplicationContext().getResources().getString(R.string.messageLang);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(this, text, duration);
toast.show();
int secs = 14; // Delay in seconds
Utils.delay(secs, new Utils.DelayCallback() {
@Override
public void afterDelay() {
Intent mStartActivity = new Intent(getApplicationContext(), SplashActivity.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);
}
});
}
public void loadLocale() {
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs",
Activity.MODE_PRIVATE);
String language = prefs.getString(langPref, "");
changeLang(getApplicationContext(), language);
}
public void changeLang(Context context, String lang) {
if (lang.equalsIgnoreCase(""))
return;
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = getApplicationContext().getResources().getConfiguration();
config.setLocale(locale);
getApplicationContext().createConfigurationContext(config);
getApplicationContext().getResources().updateConfiguration(config, getApplicationContext().getResources().getDisplayMetrics());
}
public void saveLocale(String lang) {
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(langPref, lang);
editor.commit();
}
tl;dr: I change the locale to "es", restart the activity and the app (wasn't working if I wasn't restarting the app) and save it in a sharedpreferences. Once an activity opens I retrieve the shared preferences and use it as a locale like this: First I call loadLocale(); then :
public void loadLocale() {
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs",
Activity.MODE_PRIVATE);
String language = prefs.getString(langPref, "");
changeLang(language);
}
public void changeLang(String lang) {
if (lang.equalsIgnoreCase(""))
return;
Locale myLocale = new Locale(lang);
saveLocale(lang);
Locale.setDefault(myLocale);
Configuration config = getApplicationContext().getResources().getConfiguration();
config.setLocale(myLocale);
this.createConfigurationContext(config);
this.getResources().updateConfiguration(config, getApplicationContext().getResources().getDisplayMetrics());
}
public void saveLocale(String lang) {
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(langPref, lang);
editor.commit();
}
What do I do wrong? Is there a simplier or better way to do it? I searched everywhere and nothing worked...