7

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.

kenny_k
  • 3,831
  • 5
  • 30
  • 41

1 Answers1

12

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);
}
No Body
  • 671
  • 5
  • 14
  • This stopped working after `1.2.0-alpha02` for Android 6.0.1 API 23, even though they said it's fixed in https://issuetracker.google.com/issues/140602653 :/ – Aba Mar 08 '20 at 04:29