How to Disable Dark Mode Programmatically in Android?
Last Updated :
21 Dec, 2022
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
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
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
Now in the line written under the Base Application theme
XML
< style name = "Theme.Disable DarkMode" parent = "Theme.MaterialComponents.DayNight.DarkActionBar" >
< item name = "colorPrimary" >@color/purple_200</ item >
< item name = "colorPrimaryVariant" >@color/purple_700</ item >
< item name = "colorOnPrimary" >@color/black</ item >
< item name = "colorSecondary" >@color/teal_200</ item >
< item name = "colorSecondaryVariant" >@color/teal_200</ item >
< item name = "colorOnSecondary" >@color/black</ item >
< item name = "android:statusBarColor" >?attr/colorPrimaryVariant</ item >
</ 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
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.
Share your thoughts in the comments
Please Login to comment...