2

I have seen many questions about detecting the dark mode like this one on stack overflow and visited many medium blogs like How to know when you’re using dark mode programmatically and DayNight — Adding a dark theme to your app and in all of them they perform a check like this one:

fun isNightModeEnabled(context: Context): Boolean =
    context.resources.configuration.uiMode.and(UI_MODE_NIGHT_MASK) ==
            UI_MODE_NIGHT_YES

and this works well in any phone and even the Xiaomi mobiles that run Android One but not on the Xiaomi smartphones that run MIUI.

For Xiaomi devices running MIUI:

context.resources.configuration.uiMode = 17

and context.resources.configuration.uiMode.and(UI_MODE_NIGHT_MASK) = 16

Which compared to UI_MODE_NIGHT_YES (32) always returns false with dark mode enabled or disabled.

Is it really possible to detect that the dark mode has been forced on such devices?

2 Answers 2

7

Turned out after many tests I found out it only failed when trying to disable dark mode by doing:

AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO)

In that case the method wrongly returned that it was not on dark mode. So what I did is to force disable dark mode also on the app theme:

<item name="android:forceDarkAllowed">false</item>

And this really stops dark mode for devices with MIUI.

In case you don't want to disallow dark mode you shouldn't have problems detecting the current theme by that way previously mentioned:

val currentNightMode = configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
when (currentNightMode) {
    Configuration.UI_MODE_NIGHT_NO -> {} // Night mode is not active, we're using the light theme
    Configuration.UI_MODE_NIGHT_YES -> {} // Night mode is active, we're using dark theme
}

which is the one described in the docs

2
  • I don't find this line of code <item name="android:forceDarkAllowed">false</item> anywhere. What should I do in this case?
    – Ani
    Feb 21, 2022 at 14:56
  • You should add it to the app theme you are using in case you want to force disable the dark mode. The theme the app is using is likely to be set in the android manifest in case you wonder. If it's not a custom one extend it in your styles.xml file and set the forceDarkAllowed in there and use that extended one in your activities. Feb 22, 2022 at 17:46
0

According to Chris Banes article there is another way to go for it.

Set mode (either dark or light):

AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES) // or MODE_NIGHT_NO or MODE_NIGHT_AUTO for system defaults

Then you can just play with resources, dark or light. Not sure if this way of checking dark mode from context is still relevant. I do not have that information.

Following the logic above, to check if the app is in dark mode:

if (AppCompatDelegate.getDefaultNightMode() == MODE_NIGHT_YES) //or other constants

AFAIK, the default one is MODE_NIGHT_NO

2
  • Thanks for answering, I really appreciate. However, it doesn't seem to work :( I start thinking it might be because this dark mode is forced by the OS but I don't know what could be the reason this doesn't seem to work. Dec 30, 2020 at 8:03
  • If it is forced by the OS, you should first set it to 'MODE_NIGHT_AUTO' then ask the question (if statement) Dec 30, 2020 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.