Search code examples
androidandroid-dark-thememiuidarkmode

Prevent forcing dark mode in MIUI without increasing SDK version


My new app doesn't support dark mode. When I install it on Xiaomi (with dark mode truned on) MIUI applies dark mode on it. MIUI has settings at "Settings -> Display -> More Dark mode options" (screenshot of "More Dark mode options"). This options is turned on for my app and forces dark mode despite that my app doesn't support it. Most other apps do not have this mode enabled. There are apps that are "white", but for them this mode is not enabled and they work correctly.

I found solution with adding the below line to the themes.xml:

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

The problem is that, this line requires setting minSdkVersion = 29. How to prevent MIUI enabling the option at "More Dark mode options" and forcing dark mode in my app (like in most other "white" apps) with keeping SDK version at 21?

MIUI 12 based on Android 10


Solution

  • Just copy your themes.xml file in a values-v29 folder and add <item name="android:forceDarkAllowed">false</item> only in the values-v29 variant of the file.

    If you define many things in your theme file, might be a good idea to have something like:

    values/themes.xml

    <style name="Theme.App.Base" parent="Theme.AppCompat.Light.NoActionBar">
     ... // Your attributes here
    </style>
    
    <style name="Theme.App" parent="Theme.App.Base">
    </style>
    

    values-v29/themes.xml

    <style name="Theme.App" parent="Theme.App.Base">
      <item name="android:forceDarkAllowed">false</item>
    </style>
    

    UPDATE: also, I don't think it crashes if you keep the item in your normal values folder, why do you think it's a problem in the first place? Doesn't something like this work?

    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>