If I set darkmode with AppCompatDelegate.setDefaultNightMode
and the system is not dark, then Configuration.setLocale
doesn't work.
I change the locale, for example, from En
to It
, all the strings are still displayed in the system language.
There are no problems if I set the same NightMode of the system (Android 10). The same problem with android 9 or less: if I set darkmode in my app and I change the context language, the activity displays text based on the language of the system.
Kotlin solution
override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
overrideConfiguration?.let {
val uiMode = it.uiMode
it.setTo(baseContext.resources.configuration)
it.uiMode = uiMode
}
super.applyOverrideConfiguration(overrideConfiguration)
}
Java solution
@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (overrideConfiguration != null) {
int uiMode = overrideConfiguration.uiMode;
overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
overrideConfiguration.uiMode = uiMode;
}
super.applyOverrideConfiguration(overrideConfiguration);
}