Shyam
Shyam

Reputation: 881

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?

Upvotes: 3

Views: 12893

Answers (6)

Vishv Shroff
Vishv Shroff

Reputation: 71

You can make changes in your styles.xml file.

From :

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:forceDarkAllowed">false</item>
</style>

To :

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:forceDarkAllowed">false</item>
</style>

Make sure you make changes in your both styles.xml files resides under the styles folder

Upvotes: 0

Amnah
Amnah

Reputation: 27

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.

Upvotes: 1

Ali Azaz Alam
Ali Azaz Alam

Reputation: 1868

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>

Upvotes: 4

Kleysley
Kleysley

Reputation: 573

Everytime you want to change the mode from light to night mode you have to call

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);
}

Upvotes: 0

Saikrishna Rajaraman
Saikrishna Rajaraman

Reputation: 3273

The code to set dark mode to false will work based on two factors whether the night mode is set using setDefaultNightMode() or setLocalNightMode(). The dark mode setDefaultNightMode() value will be overridden in an Activity by the value set by setLocalNightMode(). So it is always recommended to make use of the local context (Activity context) to resolve the color values instead of the Application context.

Refer to this article for more info.

DayNight - Dark mode handling

Make sure you are making use of the right context when resolving colors. If you resolve colors using the Application context its value will be different from the one resolved using the Activity context.

Resolving dark mode colors from xml. - Refer to this link.

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Read setNightMode

private UiModeManager uiModeManager;

uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE); 
uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);

Or you can add below in your style section

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

Upvotes: 0

Related Questions