Skip to content

Dark Mode (Theme)

Stanislav Osipov edited this page Oct 31, 2020 · 2 revisions

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

Determine the current system theme

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

using SA.Android.App;
using SA.Android.Content.Res;
...

// Only supported from android 10.0 (api 29)
if (AN_Build.VERSION.SDK_INT < AN_Build.VERSION_CODES.Q) {
    Debug.Log("Not supported")
    return;
}

var configuration = AN_MainActivity.Instance.GetResources().GetConfiguration();
var currentNightMode =  configuration.UIMode & AN_Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
    case AN_Configuration.UI_MODE_NIGHT_NO:
        // Night mode is not active, we're using the light theme
        Debug.Log("Night mode is not active, we're using the light theme");
        break;
    case AN_Configuration.UI_MODE_NIGHT_YES:
        // Night mode is active, we're using dark theme
        Debug.Log("Night mode is active, we're using dark theme");
        break;
}

Be notified of a change in the theme

So far it's not possible to implement a callback to be notified whenever the theme changes, but that's not a problem. User can not change the theme while your Unity application is running. So you can choose 2 strategies how you wish to support the dark theme.

  • You can check it all the time when the app is resumed, see the OnApplicationPause
  • If you do not care much about app theme immediate response (and tbh it will be not super easy to implement immediate skin change) you can only do this check during app launch.

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.

Clone this wiki locally