I tried to use night mode with changing locale in app (with version of appcompat:1.1.0 and appcompat:1.2.0 ) but when activity start again no changes were implement for locale . here is my code
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(updateBaseContextLocale(base));
}
@TargetApi(Build.VERSION_CODES.N)
private Context updateResourcesLocale(Context context, Locale locale) {
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
return context.createConfigurationContext(configuration);
}
private Context updateBaseContextLocale(Context context) {
String language = LocaleHelper.getLanguage(context); // Helper method to get saved language from SharedPreferences
Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResourcesLocale(context, locale);
}
return updateResourcesLocaleLegacy(context, locale);
}
@SuppressWarnings("deprecation")
private Context updateResourcesLocaleLegacy(Context context, Locale locale) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
I also try to add this but nothing happened
@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (overrideConfiguration != null) {
int uiMode = overrideConfiguration.uiMode;
overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
overrideConfiguration.uiMode = uiMode;
}
super.applyOverrideConfiguration(overrideConfiguration);
}
and I change theme like this AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
question from:
https://stackoverflow.com/questions/66059526/conflict-using-both-locale-and-night-mode-change-in-androidx 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…