Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

configuration.setLocale(locale) doesn't work with AppCompatDelegate.setDefaultNightMode

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.

like image 431
salvocastro Avatar asked Sep 17 '19 11:09

salvocastro


People also ask

What is an appcompatdelegate?

This class represents a delegate which you can use to extend AppCompat's support to any Activity . When using an AppCompatDelegate, you should call the following methods instead of the Activity method of the same name: The following methods should be called from the Activity method of the same name:

How do I set the system locale in Linux?

The following is a screenshot showing the warning/error when you run the dnf or yum command as shown. To set system locale, use the localectl command. For example, if you want English – UNITED STATES OF AMERICA (US) using the UTF-8 encoding, run the following command. Next, check if the system locale has been set by running the following command.

How do I set system locale using UTF-8?

For example, UNITED STATES OF AMERICA (US) English using the UTF-8 encoding is en_US.UTF-8 ). The following is a screenshot showing the warning/error when you run the dnf or yum command as shown. To set system locale, use the localectl command.

What is a locale in RHEL 8?

Note that this article should also work on any operating systems based on RHEL 8. A locale is a set of basic system parameters that define things such as a user’s language, region and any special variant preferences that the user wants to see in their user interface.


1 Answers

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);
}
like image 153
No Body Avatar answered Oct 28 '22 14:10

No Body