Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Dark Mode Programmatically in Android

I want to disable darkmode in app based on a flag Programmatically. I have defined custom colors in the values-night folder for darkmode.

I have tried the following solutions for disabling darkmode but not worked.

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

Is there any way to disable darkmode programmatically without removing values-night folder?

like image 674
Shyam Avatar asked Aug 24 '26 12:08

Shyam


2 Answers

Seems like you don't define the correct theme color. But check the below solution:

Kt.

 val mode = if ((resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) ==
                    Configuration.UI_MODE_NIGHT_NO
                ) {
                    AppCompatDelegate.MODE_NIGHT_YES
                } else {
                    AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
                }

            AppCompatDelegate.setDefaultNightMode(mode)

Colors.xml

Create a separate color file having root /values-night/color.xml and define all those colors that u already defined in /values/color.xml with your new night-theme-color codes.

Themes.xml

In /values/themes.xml define your style:

<style name="Theme.VehicleApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_700</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>

    <!-- All your theme stuff -->

    </style>

and in your /values-night/themes.xml define only those style that you want to look change in night mode otherwise don't define the same style fields that u already defined in values/themes.xml, the rest of the color manipulation work will be done by color file itself :

   <style name="Theme.VehicleApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    
    <!-- Customize your theme here. -->

   </style>
like image 143
Ali Azaz Alam Avatar answered Aug 27 '26 03:08

Ali Azaz Alam


Every time you want to change the mode from light to night mode you have to call (Don't need now, calling setDefaultNightMode is enough)

//setContentView(R.layout.YOUR_LAYOUT_NAME_FOR_THE_ACTIVITY); //this is also called in the onCreate() method

So these functions would change the theme:

private void setThemeDark(){
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    //setContentView(R.layout.YOUR_LAYOUT_NAME_FOR_THE_ACTIVITY);
}

private void setThemeLight(){
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    //setContentView(R.layout.YOUR_LAYOUT_NAME_FOR_THE_ACTIVITY);
}

From AppCompat v1.1.0-alpha05 onwards, setDefaultNightMode() will automatically apply any DayNight changes to any ‘started’ Activities. This means that you no longer have to manually recreate any Activities when you call the API.

like image 27
Amnah Avatar answered Aug 27 '26 02:08

Amnah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!