6

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

TuranM
  • 63
  • 1
  • 4

1 Answers1

14

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>
Stéphane
  • 1,518
  • 1
  • 18
  • 31
  • Thank you. But i do not want to increase version to 29. Moreover, app do not crash, but colors are mixed. It is drawing application. Whiteboard becomes black, black pen becomes white, blue pen becomes purple. – TuranM Apr 07 '21 at 07:28
  • ... who said something about increasing version to 29? You are mixing two different concepts here. Please have a look at resource modifiers. Having a values-v29 is literally the opposite of updating your minimumSdk version. In any case, take some time to implement and test my answer, I assure you that it works and that's the way to go. I successfully fixed the same thing in several apps that have their minimumSdk version set to 16. – Stéphane Apr 08 '21 at 08:10
  • Oh, you are right! false it helps without increasing to 29. thank you very much! – TuranM Apr 08 '21 at 08:46
  • 1
    My pleasure! :) – Stéphane Apr 09 '21 at 09:03