uzaysan
uzaysan

Reputation: 605

How to detect if system using dark mode?

I'm using this simple function to get dark mode. I store a boolean value in shared preferences. And If value doesn't exist, I return false for default.

This is my simple code:

public static boolean getNightMode(){
        SharedPreferences pref = getApplicationContext().getSharedPreferences("nightMode", 0);
        return pref.getBoolean("nightMode",false);
    }

Now instead of returning false for default, ı would like to return system dark mode status.

I mean if system using dark mode, return true.

How can I achive this?

Upvotes: 2

Views: 2104

Answers (3)

Amit Jayant
Amit Jayant

Reputation: 2621

To detect if "system" is in dark mode or not, you can use getNightMode() method of UiModeManager class.

https://developer.android.com/reference/android/app/UiModeManager#getNightMode()

Like so,

UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
    int mode = uiModeManager.getNightMode();
    if (mode == UiModeManager.MODE_NIGHT_YES) {
        // System is in Night mode
    } else if (mode == UiModeManager.MODE_NIGHT_NO) {
        // System is in Day mode
    }

And if want to know whether "your app" is in Night mode or not, then refer to @Lazy Ninja's answer.

Upvotes: 3

DTD
DTD

Reputation: 31

I created 2 toggle switches in my settings activity, so the one preference says whether to use the system setting (dark or light that is) or to overide. And then if it is set to overide, then I check the second preference to see if the user wants dark or light mode.

root_preference.xml

        <SwitchPreference
        app:key="@string/displayMode"
        app:title="Dark/Light Mode"
        app:summaryOn="Manual"
        app:summaryOff="Same As System"
        app:defaultValue="false" />

    <SwitchPreference
        app:dependency="@string/displayMode"
        app:key="@string/overideMode"
        app:title="Select Theme"
        app:summaryOn="Dark Mode"
        app:summaryOff="Light Mode"/>

MainActivity.java (in onCreate)

        SharedPreferences userPref = androidx.preference.PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    //Use preference to set night or light mode
    if(userPref.getBoolean(getString(R.string.displayMode),false)){
        if(userPref.getBoolean(getString(R.string.overideMode), true)){
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
    }else{
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    }

Upvotes: 0

Lazy Ninja
Lazy Ninja

Reputation: 22537

You can use the Configuration settings.

int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
    case Configuration.UI_MODE_NIGHT_NO:
        // Night mode is not active, we're using the light theme
        break;
    case Configuration.UI_MODE_NIGHT_YES:
        // Night mode is active, we're using dark theme
        break;
}

For more details, please refer to the developer site.

Kotlin:

val currentNightMode = configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
when (currentNightMode) {
    Configuration.UI_MODE_NIGHT_NO -> {} // Night mode is not active, we're using the light theme
    Configuration.UI_MODE_NIGHT_YES -> {} // Night mode is active, we're using dark theme
}

Upvotes: 4

Related Questions