Skip to main content

Overview

Checks the current day/night theme status of the device. This determines whether dark mode is enabled or disabled. Available on Android 9+ (API level 28).

Method Signature

DynamicColor.dayNight(callback)

Parameters

callback
function
required
Callback function that receives the current theme statusCallback Parameters:
  • theme (string): Either 'light' or 'dark'

Returns

theme
string
Returns the current theme mode:
  • 'light' - Light/day theme is active
  • 'dark' - Dark/night theme is active

Example

DynamicColor.dayNight(function(theme) {

	if(theme == 'light') {
		// Light theme is active
		console.log('Device is in light mode');
	} else { // theme == 'dark'
		// Dark theme is active
		console.log('Device is in dark mode');
	}

});

Use Cases

Apply Theme-Specific Styles

DynamicColor.dayNight(function(theme) {
	if(theme == 'light') {
		document.body.classList.add('light-theme');
		document.body.classList.remove('dark-theme');
	} else {
		document.body.classList.add('dark-theme');
		document.body.classList.remove('light-theme');
	}
});

Listen for Theme Changes

document.addEventListener('dynamicColorChange', function(event) {
	if(event.changed.dayNight) {
		DynamicColor.dayNight(function(theme) {
			console.log('Theme changed to:', theme);
			// Update your UI accordingly
		});
	}
});

Implementation Details

  • Platform Support: Android 9+ (API level 28)
  • Native Implementation: Checks Configuration.UI_MODE_NIGHT_MASK flag
  • Source Reference: plugin.js:160-165, DynamicColor.java:89-105
This method is independent of Dynamic Color availability and works on Android 9+, while Dynamic Color requires Android 12+.

Build docs developers (and LLMs) love