Is your Android app using Dark Mode against your will?
Did you know it was possible for some Android devices to force the use of Dark Mode on their apps? This means if you are developing an Android application, it could be forced to use Dark Mode against your will.
In this quick article, I will explain how this might happen to you, and show you the simple solution on how to prevent this behavior.
How popular is Dark Mode?
Dark Mode (called Dark Theme in Material’s official documentation), is a display setting to show some light-colored text/icons on a dark background and became available with Android 10. Benefits of switching to Dark Mode include better visibility of what’s on the screen, especially in low-light environments, but also a decrease in power usage.
If you’re reading this, chances are you’re a tech-savvy person, and that you’ve been using Dark Mode for a while now. Some surveys I found online seem to indicate that a majority of us have adopted it.
However, it’s harder to estimate how many users globally are actually using it. I’ve asked some of my family members who are a lot less interested in technology than I am, and they had no idea what I was talking about 🤷. I couldn’t find official numbers from Google or Apple on how many users are using Dark Mode on their devices.
Is your app using Dark Mode without your knowledge?
Regardless of how many users are actually using Dark Mode, the fact is that many app developers are gradually adopting it, and if you’re an app developer yourself you’ve probably asked yourself this question: should I implement Dark Mode on my app?
At my current company, we’ve talked about it a few times, but we still haven’t made the switch. Our users are not particularly tech-savvy so their interest in it is low, and our main competitors haven’t made the switch either, therefore we decided to put our efforts elsewhere.
I have Dark Mode enabled on my phone, and our app is being displayed properly in Light Mode on it, so we’ve assumed that we didn’t need to do anything special while we didn’t implement on Dark Mode.
But then we recently had a new hire join the team, and he showed us that on his phone the app was forced into using Dark Mode! And the results were… not pretty…
As you can see, we had a number of problems with icons being in the wrong color, some of them being a shade of dark on dark. And generally, we had a lot of problems with contrasting the different elements with the background. And this is just the home screen of the app!
What happened here is that the phone of my colleague was forcing the Dark Mode on all apps present on it, and our app was a victim of this behavior. But, some other apps on his phone were resisting, still showing their UI in a light version. Which meant there was something we could do to fix it.
But first, let’s reproduce the issue.
Can Dark Mode be forced on your app?
To check whether or not your app would be forced into using Dark Mode, no need to go buy the same phone as my colleague: you can use the Developer Options to force the Dark Mode on any device running Android 10 or higher, whether it is a physical device or an emulator.
Navigate to the Developer options on the device you want to test on (if you’ve never enabled them, click 7 times on Build number in the About phone settings), and look for “Override force-dark”.
Turn the option on then open your app, and find out whether or not the Dark Mode has been forced upon it!
How to disable the forced Dark Mode?
Thankfully, if your app is also being forced to used Dark Mode, there is a very simple way to prevent it from happening.
You can use the API
AppCompatDelegate.setDefaultNightMode()
documented here to set the night mode you want to use.
You can set it to the following values:
- MODE_NIGHT_NO → always use Light Mode
- MODE_NIGHT_YES → always use Dark Mode
- MODE_NIGHT_FOLLOW_SYSTEM → use what the system uses (default)
As it is set to MODE_NIGHT_FOLLOW_SYSTEM by default, if the device forces dark mode on you, then your app will obey.
Simply set it to MODE_NIGHT_NO and your app will now always be in Light Mode until you decide to properly implement the Dark Mode functionality (at which point you can let the user decide which mode to use via this API).
You can call this API in your Application class, so it is executed every time you open your app (the value you set here is not persisted across process starts, which means it needs to be set every time).
Your code will now look like this:
I hope this article was useful to you, and that you now have a better understanding of how your Android application could be forced into using Dark Mode, how to check if it is the case, and how to prevent that from happening.
To read more on Dark Mode for Android:
- Official documentation
- Article on the topic, published in the official Android Developers Medium publication.
Happy coding!