Open In App

How to Disable Dark Mode Programmatically in Android?

Last Updated : 21 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to disable Dark Mode programmatically in Android. Changing the theme of your app is a simple task that includes changing the code in the XML file.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

Create a new project by clicking on the file and selecting “New Project”. Now, select an empty activity and press the Next button.

Create new Project

Create new Project

In this step, you will be asked to set the name of your application and the minimum version on which it could be used, select it and click Finish.

Setting the name of the application

Setting the name of the application

After this, a default application is created with a text displaying “Hello World” on the screen.

Step 2: Changing the theme

Open the “themes.xml(night)” file under app->res->values->themes->themes.xml(night).

XML file

XML file

Now in the line written under the Base Application theme

XML




<resources xmlns:tools="http://schemas.android.com/tools">
   <!-- Base application theme. -->
   <style name="Theme.Disable DarkMode" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
       <!-- Primary brand color. -->
       <item name="colorPrimary">@color/purple_200</item>
       <item name="colorPrimaryVariant">@color/purple_700</item>
       <item name="colorOnPrimary">@color/black</item>
       <!-- Secondary brand color. -->
       <item name="colorSecondary">@color/teal_200</item>
       <item name="colorSecondaryVariant">@color/teal_200</item>
       <item name="colorOnSecondary">@color/black</item>
       <!-- Status bar color. -->
       <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
       <!-- Customize your theme here. -->
   </style>
</resources>


Change parent from

XML




<style name="Theme.Disable DarkMode" parent="Theme.MaterialComponents.DayNight.DarkActionBar">


 

to

XML




<style name="Theme.Disable DarkMode" parent="Theme.MaterialComponents.Light.DarkActionBar">


Disabling Dark Mode

Disabling Dark Mode

After this, build your application, and now the dark mode has been disabled in your application, and the application will run in light mode even though your device is set to dark mode.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads