Setting up Dark Theme for Your App

Google have introduced native support of system-wide dark themes in Android 10. And with it rolling out in more and more devices every month, the need for Dark mode for your app is on-rise and demand. This article is going to explain on how you can set up dark theme for your next app.

The Benefits of Dark Theme

Before starting, let’s see what are the benefits of using dark theme for your Android application. The dark mode helps you with the following benefits.

  • Reduces the battery usage of the device.
  • Reduces eye strain, helping you in using phone in night.
  • Facilitates the use in low-light environments.

Forcing App to Dark Mode Automatically

Ideally, your app should have support for all modes like light, dark, and system. If you’re in a hurry and want to implement dark-theme without spending more time on app, then you can do so with a very quick and short way.

By default, switching your phone theme to dark, you will notice that your app UI won’t change to dark colors. If you want to automatically change colors of the app by the Android system, you can do so by enabling override force-dark in Developer options. Here’s how you can enable force dark mode in the app code.

Go to your styles.xml theme file and set android:forceDarkAllowed to true like this:

<!-- Default Theme -->
<style name="Theme.Imagine.Base" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
   <!-- Primary brand color. -->
   <item name="android:forceDarkAllowed" tools:targetApi="q">true</item>
   <item name="colorPrimary">@color/color_primary</item>
   <item name="colorPrimaryVariant">@color/color_primary_dark</item>
   <item name="colorOnPrimary">@color/color_on_primary</item>

For some apps, this solution might be sufficient, for many it won’t. Colors might feel off brand, contrasts between components might become too low, and it might even be confusing to users if they don’t recognize the design due to a totally unknown color scheme.

This is where the actual proper way comes to help to add the fully functional dark-mode support for your app.

Save and Load User’s Preferences for Dark/Light Theme

A developer-friendly solution of customizing styles and colors would be using the Day Night theme at first. This will run on the devices with older versions than Android 10 as well and provide you backwards compatibility.

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   <!-- Customize your theme here. -->

Now run your app and if you are using a device running (Android 10) then your application will change its theme according to Android system. It will show as dark in dark theme etc. But if you are using an older device like Lollipop or Oreo, then you need to manually provide a toggle in-app setting to change the app theme, or you may say you would need to programmatically change the colors of the app.

A good approach for this setting would be to add a dialog like this and add the following three options.

An example dialog for light dark mode options An example dialog for light dark mode options

The Android OS provides APIs to set the theme based on the above-mentioned options.

  • MODE_NIGHT_AUTO : This option is usually configured by the Android OS system through the Display Settings. Users often set times to switch between light and dark themes, and that’s when this option is used. For your app, the dialog above will show the Follow System and it means app will show the theme which system provides, and it could be light or dark depending on the time of device.
  • MODE_NIGHT_YES : This is for setting the app to dark theme by default no matter what time or configuration device is set on.
  • MODE_NIGHT_NO: This means the device will not be in the dark mode by default. This doesn’t mean that device will be in light though. It can also mean that if the device is configured to dark/light schedule, then it will also be followed with this option. This is used for Light and Follow System options.
  • MODE_NIGHT_AUTO_BATTERY. This is an extra option which is not put in the settings dialog (as seen in the above image). Setting this mode mean that the app will always be in light mode regardless of whether its configured or not. It will only show dark theme when the device’s battery is low and user has turned on the battery saver.

So, the idea to respect the app user’s choice of theme, you have to store the user selected option (from dialog like above) and save it somewhere persistent like SharedPreferences. You only will have to save the integer like AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY, AppCompatDelegate.MODE_NIGHT_NO etc. And when the app starts, load that value from persistent storage and set it as the default mode of the app with the method AppCompatDelegate.setDefaultNightMode(). This is how you can save and load the theme which has user has opted for.

Managing Resources like Colors, Images, Drawables

Once your app is able to save/load user’s preferences of theme choice, it’s time for your app to show right resources like colors, images, drawables etc. in the right theme. You don’t want to show dark colors in the dark theme. Or you don’t want to show images with bright backgrounds in the dark theme.

To manage this, Android provides you a way to separate resources for light and dark themes. Normally, you put all your colors in the colors.xml file in the res/colors directory. But these colors will be used for light theme and also dark theme if there are no colors provided for that. So, you would need to create another directory with the -night suffix like colors-night in the res folder and create another colors.xml file and put all the color values for the dark theme here.

Adding Night Colors in Android Studio Adding Night Colors in Android Studio

It must be noted that both color.xml files in /colors and /colors-night directories should contain different color values but same names. Like for example, if you have a CardView in the code, you set it’s background color to card_background_color. The card_background_color will be added in the colors.xml files like this

<!-- res/colors/colors.xml -->
<item name=”card_background_color”>#FFF</item>

<!-- res/colors-night/colors.xml-->
<item name=”card_background_color”>#000</item>

And now you can use it for your CardView just like you use any normal color.


Now when you run the app, you will see that in light mode, CardView will be white and in dark mode CardView will be black. Android Studio will show you both colors.xml files in a combined directory like this.

Colors dark and light directories in Android Studio Colors dark and light directories in Android Studio

You can use the same method to separate your custom XML shape drawables or vector drawables by creating a directory res/drawables-night and putting all the night mode drawbales there. For example, here’s how a Vector icon with both dark and light asset will show in Android Studio.

Vectors dark and light directories in Android Studio Vectors dark and light directories in Android Studio

Again, you have to use the exact same file name for both icons. And you can use that normally for your ImageViews etc like app:src=”@drawable/ic_baseline_settings”, You can also put different images in drawables and drawables-night directories. You can also apply this for your strings as well where you want to show different message for day and night.

Android Studio also allows you to view your XML layouts in Dark/Light modes from the editor itself by choosing an option from a moon-like icon. In the below image, you can see a layout in both Dark and Light modes in the XML editor of Android Studio.

Light and Dark modes in Layouts in Android Studio Light and Dark modes in Layouts in Android Studio

That’s how you can implement dark mode for your app. You can check an example app Imagine and how the colors and resources are used for the both modes in the following Github Imagine repository.

Light and Dark modes in Layouts in Android Studio Light and Dark modes in Layouts in Android Studio

You can check the code of this whole app on the following repository.

At the end, please Subscribe to my newsletter DroidUp to learn learn about the latest things, tips, and skills in Android development manually handcrafted and curated by Wajahat Karim.

If you liked this article, you can read my new articles below:

profile card
Wajahat Karim
🌍 Making the world a better place, one app at a time.
🔥 Google Developer Expert (GDE) in Android . 📱 Professional Android Developer with ~10 years experience. 💻 Creator of various Open Source libraries on Android . 📝 Author of two technical books and 100+ articles on Android. 🎤 A passionate Public Speaker giving talks all over the world.
Author's picture

Wajahat Karim

🔥 Google Dev Expert (GDE) in Android .
📱 Android Dev. 💻 Open Source Contributor . 📝 Technical Writer . 🎤 Public Speaker

Senior Android Developer

Karachi, Pakistan