Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
How to Change the Color of Status Bar in an Android App?
Next article icon

How to Check if An App is In Dark Mode and Change it To Light Mode in Android?

Last Updated : 07 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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:


Next Article
How to Change the Color of Status Bar in an Android App?
author
chinmaya121221
Improve
Article Tags :
  • Android

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 t
    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 enforcing a specific theme, either Light or Dark regardless of the user's device settings. This is crucial for maintaining a smooth user experience or aligning your application's branding. You may w
    4 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences