49

I am looking for a way to enable night mode programmatically with an Android code:

public static void setNightMode(Context target, boolean state){

    UiModeManager uiManager = (UiModeManager) target.getSystemService(Context.UI_MODE_SERVICE);

    if (state) {
        //uiManager.enableCarMode(0);
        uiManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
    } else {
        // uiManager.disableCarMode(0);
        uiManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
    }
}

Nothing has changed on my screen, the night mode is still disables. According to this link

There is no need to enable carMode or deskMode. I have the following logcat on Android Studio:

11-26 12:15:16.662 3823-3823/? D/UiModeManager: updateConfigurationLocked: mDockState=0; mCarMode=false; mNightMode=2; uiMode=33
11-26 12:15:26.802 3823-3823/? V/UiModeManager: updateLocked: null action, mDockState=0, category=null
4
  • try this stackoverflow.com/questions/31934503/…
    – Munir
    Nov 26, 2017 at 11:34
  • I want to change night mode everywhere ,not in my app.I don't need to switch some themes.
    – Ilan
    Nov 26, 2017 at 11:44
  • Did you check this note? "Note: On API 22 and below, changes to the night mode are only effective when the car or desk mode is enabled on a device. Starting in API 23, changes to night mode are always effective."
    – Google
    Mar 29, 2018 at 6:00
  • Were you able to solve this issue? I am struggling with setNightMode and I am not able to get it on Android 10 or 11
    – Ton
    Nov 19, 2020 at 8:28

7 Answers 7

85

SIMPLEST SOLUTION

You can enable/disable application's dark theme just by:

  1. enable dark theme:

     AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
    
  2. forcefully disable dark theme:

     AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
    
  3. set app theme based on mobile settings of dark mode, i.e. if dark mode is enabled then the theme will be set to a dark theme, if not then the default theme, but this will only work in version >= Android version Q (10)

     AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
    

Notes:

  1. Your base theme for app/activity should be

"Theme.AppCompat.DayNight"

like

<style name="DarkTheme" parent="Theme.AppCompat.DayNight">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
  1. Your res folder's names would end with -night so that different colors and images you can set for day and night themes like

drawable & drawable-night,
values & values-night

3
  • 2
    Unless I am wrong, I believe OP was looking for a way to change night mode device-wise. Your code works perfectly fine app-wise. As I was looking for a way to change for the whole device, UiManager seems the right class to do so.
    – Mackovich
    Aug 24, 2020 at 15:49
  • Works perfectly for me! Nov 11, 2020 at 8:25
  • Works perfectly for me, too!
    – yoonhok
    Dec 15, 2020 at 23:20
18

Make sure to change the default theme from Theme.AppCompat.Light.DarkActionBar to Theme.AppCompat.DayNight.DarkActionBar in the styles.xml file and then do AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) to switch to the night mode. I have tested it in APIv23(Android 6.0) and above and it is working fine. For a better explanation see this codelab by Android

1
  • Hello Neeraj Sewani. I am trying to set an app that simply toggles the value of Night mode in the whole device. I am using uiManager.setNightMode(UiModeManager.MODE_NIGHT_YES) or MODE_NIGHT_NO. But I am not able to get it. Do I also have to change the "default theme" in my app? I don't mind how my app is displayed. I just want to change the device Night mode. Thanks.
    – Ton
    Nov 19, 2020 at 8:32
6

This code is working perfectly for me, though you may just need to restart your app. But keep in mind, that this code enables the Dark Mode system-wide, not just in the app:

public static void setNightMode(Context target , boolean state){

    UiModeManager uiManager = (UiModeManager) target.getSystemService(Context.UI_MODE_SERVICE);

    if (VERSION.SDK_INT <= 22) {
        uiManager.enableCarMode(0);
    }
    
    if (state) {
        uiManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
    } else {
        uiManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
    }
}

This worked for me in Kitkat.

2

Remember that Dark Mode is not Night Mode. They are completely different. DM was introduce in Android 10 that enforce built-in black and white color while NM was release in earlier version that uses either default/custom style commonly called as Resource Qualifiers. If you want to handle your own custom light/night style and not relying on Android's built-in dark style, you may want to set forceDarkAllowed to false in themes.xml or style.xml to avoid conflict.

To change your app mode to night you use AppCompatDelegate.

// This will be the top level handling of theme
        AppCompatDelegate.setDefaultNightMode(
            if (userPrefModeIsNight)
                AppCompatDelegate.MODE_NIGHT_YES
            else
                AppCompatDelegate.MODE_NIGHT_NO)
0

One more thing: your Activity needs to extend from AppCompatActivity.

If it extends the plain Activity, then calling AppCompatDelegate.setDefaultNightMode will not work.

0
int nightModeFlags =
getContext().getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK;
switch (nightModeFlags) {
case Configuration.UI_MODE_NIGHT_YES:
     doStuff();
     break;

case Configuration.UI_MODE_NIGHT_NO:
     doStuff();
     break;

case Configuration.UI_MODE_NIGHT_UNDEFINED:
     doStuff();
     break;
}
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jul 27, 2022 at 20:28
-1

NightOwl has its own implementation for switching day/night mode on Android. Getting started with NightOwl is super easy. Here's a code snippet:

Init the NightOwl in Application class,

NightOwl.builder().defaultMode(0).create();

Call three method in your Activity class,

public class DemoActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // step1 before super.onCreate
        NightOwl.owlBeforeCreate(this);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);

        // step2 after setContentView
        NightOwl.owlAfterCreate(this);

        // write your code
    }

    @Override
    protected void onResume() {
        super.onResume();

        // step3 onResume
        NightOwl.owlResume(this);
    }

}

Switch skin everywhere as you like,

View v = findViewById(R.id.button);
v.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        NightOwl.owlNewDress(SettingActivity.this);
    }
});
1
  • Google libraries will be the best choice
    – Vlad
    Jul 28, 2020 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.