Is there an API to detect which theme the OS is using - dark or light (or other)?

Google has just published the documentation on the dark theme at the end of I/O 2019, here.

In order to manage the dark theme, you must first use the latest version of the Material Components library: "com.google.android.material:material:1.1.0-alpha06".

Change the application theme according to the system theme

For the application to switch to the dark theme depending on the system, only one theme is required. To do this, the theme must have Theme.MaterialComponents.DayNight as a parent.

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    ...
</style>

Determine the current system theme

To know if the system is currently in dark theme or not, you can implement the following code:

switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
    case Configuration.UI_MODE_NIGHT_YES:
        …
        break;
    case Configuration.UI_MODE_NIGHT_NO:
        …
        break; 
}

Be notified of a change in the theme

I don't think it's possible to implement a callback to be notified whenever the theme changes, but that's not a problem. Indeed, when the system changes theme, the activity is automatically recreated. Placing the previous code at the beginning of the activity is then sufficient.

From which version of the Android SDK does it work?

I couldn't get this to work on Android Pie with version 28 of the Android SDK. So I assume that this only works from the next version of the SDK, which will be launched with Q, version 29.

Result

result


A simpler Kotlin approach to Charles Annic's answer:

fun Context.isDarkThemeOn(): Boolean {
    return resources.configuration.uiMode and 
            Configuration.UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES
}