Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The method for identifying the device theme on Android is by using the following code:

int nightModeFlags = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (nightModeFlags) {
    case Configuration.UI_MODE_NIGHT_YES:
        // Night mode is on
        break;
    case Configuration.UI_MODE_NIGHT_NO:
        // Night mode is off
        break;
    case Configuration.UI_MODE_NIGHT_UNDEFINED:
        // Night mode is undefined
        break;
}

This code uses the getResources() method to get the configuration of the device's resources. Then, it checks the uiMode flag to determine whether the device is in night mode or not. If the device is in night mode, the UI_MODE_NIGHT_YES value will be returned. If the device is in day mode, the value will be UI_MODE_NIGHT_NO. If the night mode is undefined, the value returned will be UI_MODE_NIGHT_UNDEFINED.