I'm changing the app locale at the press of a button. Which works perfect on AVDs as well as debug & release build APKs on actual devices with API 30.
But however it is not working with the Play Store version of the app once it is published. The locale never gets changed.
Please help! Thank you!
This is the code in the SettingsFragment:
private void setAppLocale(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
Intent refresh = new Intent(getActivity().getApplicationContext(), MainActivity.class);
startActivity(refresh);
getActivity().finish();
}
The above gets called once a button is pressed as well as the selections gets put in Shared Preferences. The activity gets refreshed & the main activity loads but the locale is never changed.
This is how my MainActivity looks like:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hideSystemUI();
sharedPref = getPreferences(Context.MODE_PRIVATE);
selectedLanguage = sharedPref.getString("Test.SL.LanguageName", language);
selectedTheme = sharedPref.getString("Test.SL.ThemeName", "Light");
if (selectedTheme.equals("Light")){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else if (selectedTheme.equals("Dark")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
if (selectedLanguage.equals("Sinhala")) {
language = "Sinhala";
setAppLocale(this, "si");
} else {
language = "English";
setAppLocale(this, "en");
}
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarMain.toolbar);
......
}
public void setAppLocale(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
Any ideas, suggestions & solutions please! Thank you again!