How to Create a Dark Mode for a Custom Android App in Kotlin?
Last Updated :
14 Oct, 2020
The dark mode is a feature that allows you to switch the color theme of an app or a whole OS to black or something on the brink of it. Beyond the joys of invigorating a tired design, one may wish to choose because it makes watching the device screen way more comfortable and relaxing on the eyes. Typical pixel values during the regular mode fall between 200 to 255. Each pixel emitting light at 255 value corresponds to the maximum possible light it can emit, providing more power. Similarly, a 0 value corresponds to the minimum amount, which corresponds to no power being supplied to the pixel. If one uses a smartphone that has an OLED display and enables dark mode, it might save some battery life too. So it becomes essential for a developer to add such a feature in the desired application. This article wants to share with you the implementation of dark mode in Android by using available libraries. Dark Mode is an example of optimization of User Experience as well as the Battery. It can be implemented on any application a developer desires. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language.

Approach
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Changes made to styles.xml file
Go to the res > values > styles.xml file and change the style parent to "Theme.AppCompat.DayNight.DarkActionBar". Below is the complete code for the styles.xml file.
XML
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
Step 3: Working with the activity_main.xml file
Now go to the activity_main.xml file which represents the UI of the application, and create a Switch. This switch shall toggle between the dark mode and normal mode. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--Create a switch-->
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Enable dark mode"
tools:ignore="UseSwitchCompatOrMaterialXml" />
</RelativeLayout>
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file, and refer the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.os.Bundle
import android.widget.Switch
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declare the switch from the layout file
val btn = findViewById<Switch>(R.id.switch1)
// set the switch to listen on checked change
btn.setOnCheckedChangeListener { _, isChecked ->
// if the button is checked, i.e., towards the right or enabled
// enable dark mode, change the text to disable dark mode
// else keep the switch text to enable dark mode
if (btn.isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
btn.text = "Disable dark mode"
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
btn.text = "Enable dark mode"
}
}
}
}
Output: Run on Emulator
Similar Reads
How to Create a Custom Intro Slider of an Android App? Intro Slider in many apps is mostly used to educate the users about the app, the features of the app, and the services that our app will provide to us. In this article, we will take a look at the implementation of Custom Intro Slider in our app. What we are going to build in this Article? We will be
8 min read
Create Custom Intro Slider of an Android App with Kotlin IntroSlider is seen in many android applications to display the introduction of the different screens within our application. This intro slider will display information about different features within the android application. In this article, we will take a look at How to Create a Custom Intro Slide
8 min read
Step-by-Step Guide to Creating a Custom Spinner in Kotlin for Android In Android development, a Spinner allows users to select an item from a dropdown menu. While the default ArrayAdapter provides basic functionality, customizing a Spinner can enhance its appearance and usability. This article will guide you through creating a custom Spinner using Kotlin. You'll learn
4 min read
How to Check if An App is In Dark Mode and Change it To Light Mode in Android? In this article, we are going to first check whether the application is in Dark mode or not it the app is in Dark mode then we have to change the app to light mode. A sample video is given below to get an idea about what we are going to do in this article. Here we have to simply follow two steps. 1.
3 min read
How to change the color of Action Bar in an Android App? Customizing the Action Bar allows you to enhance the visual appeal of your Android app. In this article, you will learn how to change the colour of the Action Bar in an Android App. Basically, there are two ways to change color.By changing styles.xml file:Just go to res/values/styles.xml fileedit th
2 min read
A Complete Guide to Learn Kotlin For Android App Development Kotlin is a statically typed, cross-platform, general-purpose programming language for JVM developed by JetBrains. This is a language with type inference and fully interoperable with Java. Kotlin is concise and expressive programming as it reduces the boilerplate code. Since Google I/O 2019, Android
8 min read
How to implement Dark (Night) mode in Android app Light-on-dark color scheme, also called dark mode, is a supplemental mode that uses a color scheme in which content of a webpage is displayed on a dark background. Such a color scheme reduces the light emitted by screens and enhances readability. Switching to dark mode allows website users to move t
5 min read
How to Change the Color of Status Bar in an Android App? A Status Bar in Android is an eye-catching part of the screen, all of the notification indications, battery life, time, connection strength, and plenty of things are shown here. An Android user may look at a status bar multiple times while using an Android application. It is a very essential part of
4 min read
How to Create Shine Effect in Android? Shine Effect is used to give an ImageView, Button, or a View a better animation look. It is very easy to implement. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language.  Step by Step Imp
3 min read
How to Create a Wallpaper App in Android Studio? Almost all Android devices are having a wallpaper set on their home screen. For setting this wallpaper to the screen many Android devices provides a Wallpaper Application where we can browse different types of wallpapers based on various categories. In this article we will look at, building a simila
15+ min read