How to Check if An App is In Dark Mode and Change it To Light Mode in Android?
Last Updated :
07 Jun, 2023
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. Check if dark mode is enabled:
val DarkModeFlags = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK// Retrieve the Mode of the App.
val isDarkModeOn = DarkModeFlags == Configuration.UI_MODE_NIGHT_YES//Check if the Dark Mode is On
2. Change to light mode if dark mode is enabled:
if (isDarkModeOn) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)//Switch off the dark mode.
}
Step-by-Step Implementation
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 and select the language as Kotlin.
Step 2: Change the StatusBar Color
Navigate to app > res > values > themes > themes.xml and add the below code under the style section in the themes.xml file.
<item name="android:statusBarColor" tools:targetApi="l">#308d46</item>
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. It represents the main UI of our App, the UI Contains one TextView to display whether the app is in dark mode or in light mode, then a Button to change the mode to Light mode.
activity_main.xml File:
XML
<?xml version="1.0" encoding="utf-8"?>
<!--LinearLayout-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<!--TextView-->
<TextView
android:id="@+id/tv_modeDisplay"
android:textStyle="bold"
android:gravity="center"
android:textColor="#308d46"
android:padding="10dp"
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!--Button-->
<Button
android:id="@+id/btn_changeToLightMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change to Light Mode"
android:visibility="gone"
tools:visibility="visible"
android:layout_marginTop="15dp"/>
</LinearLayout>
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt and follow the below code. Below is the code for the MainActivity.kt. Comments are added inside the code to understand the code in more detail. In this activity we are going to check if the app is in dark mode or not if in dark mode then we are going to change it to Light mode.
MainActivity.kt File:
Kotlin
package com.example.gfg
import android.content.res.Configuration
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatDelegate
import kotlin.properties.Delegates
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val tv_displayMode:TextView=findViewById(R.id.tv_modeDisplay)
val btn_changeToLightMode:Button=findViewById(R.id.btn_changeToLightMode)
// Check if the app is in dark mode
if(isDarkModeOn()){
// Display the message for dark mode
tv_displayMode.text="This App is in Dark Mode,Click the below button to change into Light Mode"
btn_changeToLightMode.visibility=View.VISIBLE
// Set Onclick listener for changing to light mode
btn_changeToLightMode.setOnClickListener {
changeToLightMode()
}
}else{
tv_displayMode.text="This App is already in Light Mode."
}
}
// check if the app is in dark mode or not
fun isDarkModeOn(): Boolean {
val nightModeFlags = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
val isDarkModeOn = nightModeFlags == Configuration.UI_MODE_NIGHT_YES
return isDarkModeOn
}
// If the App is in Dark Mode then
// change it to Light Mode
fun changeToLightMode(){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
}
Output:
Similar Reads
How to Check if Application is Installed in Your Android Phone and Open the App?
In this article, we are going to check if a particular app is installed on our phones or not. If yes then we will be having an option to open the app. Else it will show a toast message saying Not available. So here we are going to learn how to implement that feature. Note that we are going to implem
3 min read
How to Create a Dark Mode for a Custom Android App in Kotlin?
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. Typ
3 min read
How to Change Text Color of Toolbar Title in an Android App?
In an Android app, the toolbar title present at the upper part of the application. Below is a sample image that shows you where the toolbar title is present. In the above image, you may see that the color of the Toolbar Title is white which is by default. So in this article, you will learn how to ch
2 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
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 Check if the Android Device is in Dock State?
A docking station is a device that is competent in communicating with the Android kernel to fire docking-related events and revise the docking file state. A docking station can make the system and apps do anything that is programmed. One example is showing a different layout on the docked state. It
4 min read
How to Build a Palindrome Checker App in Android Studio?
In this article, we will be building a Palindrome Checker android app in Android Studio using Java/Kotlin and XML. The app will check whether the entered word is Palindrome or not, if the entered word is a Palindrome then a toast will be displayed having the message "Yes, it's a palindrome" otherwis
3 min read
How to Check if the Battery is Charging or Not in Android Programmatically?
The charging status can change as quickly as a device can be plugged in, so it's crucial to monitor the charging state for changes and alter your refresh rate accordingly. The Battery Manager broadcasts an action whenever the device is connected or disconnected from power. It is important to receive
3 min read
How to Force an Android App to Run in Light or Dark Mode Regardless of the Device Setting
When developing an Android Application, you may want to control the appearance of your application by forcing a specific theme, either Light or Dark regardless of the user's device settings. This is mandatory for maintaining a smooth user experience or aligning your application's branding. You may w
3 min read
How to Change App Icon of Android Programmatically in Android?
In this article, we are going to learn how to change the App Icon of an App on the Button Click. This feature can be used when we have an app for different types of users. Then as per the user type, we can change the App Icon Dynamically. Step by Step Implementation Step 1: Create a New Project To
3 min read