Dark mode, nowadays, is one of the most demanding features the people are seeking in both mobile and web applications. It’s a healthy option and the device emits less blue light while using Dark mode; I am damn sure all are obsessed with Dark Theme.
With the introduction of Android 10, Google brought the much-awaited Dark Mode to the native Android framework.
I didn’t find any complete package of Dark Theme for Button Color, Text Color, and more importantly, the image changes while converting into Dark Theme on Web. That’s why I decided to bring all together in one place and provide a complete walkthrough of Dark Theme implementation in Android Application.
Why do we need a Dark Theme?
It just looks great with few other advantages like,
Can fundamentally reduce power utilization for devices with OLED display. That is the reason a few makers empower Dark Theme as a piece of the Battery Saver mode.
Bring much more comfortable to use your device in a low-light environment.
It is improving visibility for users with low or limited vision and those who are sensitive to bright light.
The Dark mode is a huge help for battery life on android.
Adding the Dark theme to your Android Application
Now, we will be discussing how to add Dark Theme to Android Application. Let’s start the implementation!!!
1. Declare Dependencies
Adding the following dependencies to your build.gradle(Module: app)
2. Adding a theme to our styles.xml
Let’s replace the current android theme in our application with the “DayNight” theme.
styles.xml
Note: If you want to replace the color in the app, you need to create a custom values directory, called values-night, and for image resources as well we will create drawable-night.
3. Creating Directories and xml files
Before adding the custom theme values to our application, we have to create new directories and xml files to implement Dark mode.
Create values-night directory
To do this, right-click on the res/values folder and select “New -> Directory”. The directory name should be “values-night”.
Create styles.xml (night)
After creating the “values-night” directory, we have to create styles.xml for night mode to declare the Dark Theme style.
Change your project structure to “Project” mode and right-click on the values-night folder and select “New -> Values Resource File”. The name of the values xml file should be “styles.xml”.
Create colors.xml (night)
In advance, create the “colors.xml” values file for colors night mode. Follow the previous step to create the “colors.xml” file. The below image shows the structure of the “values” folder.
Create drawable-night directory
Right-click on the “res” folder and select “New -> Directory”. The directory name should be like “drawable-night”.
Now, we created all the pre-required directories and xml to implement the Dark Theme.
Android Project Structure For Dark Mode
4. Sample Code For Android Dark Theme
I write a simple code for the activity_main.xml class.
We have to set a custom text color and drawable on the ImageView.
The drawable filename, colors, style names need to be equal in both the directories for the assets you desire to toggle in DayNight theme.
Text Color and Drawable Image will change depending on the device theme. Next, we move to set up the styles for both the Day-Night themes.
The code for styles.xml for values and values-night folders are given below.
styles.xml
styles.xml(night)
The same for colors.xml for values and values-night folders are given below.
colors.xml
colors.xml(night)
The code for the MainActivity.java class
Note: Don’t forget to add the below code when you are using an alert dialog box.
ContextThemeWrapper helps you to change the dialog box color depending on your Day-Night theme.
In the above code, we use the Switch to toggle between the day and night mode themes in our application. We save the current mode in a SharedPreference object. Use of Shared Preference, we retrieve the theme mode every time we open an application.