0

Officially dark theme is available in Android 10, but in reality, dark theme available in Android 9 MIUI 11 for example. Is there a way to check programmatically if OS dark theme available or not? I tried to check it through the Configuration#uiMode field:

public boolean isSystemDarkAllowed() {
    final Configuration configuration = context.getResources().getConfiguration();
    final int nightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
    return nightMode != Configuration.UI_MODE_NIGHT_UNDEFINED;
}

but it is not working, for Android 5 it returns true

Alexey Nikitin
  • 604
  • 7
  • 22
  • possible duplicate of https://stackoverflow.com/questions/40357331/detect-the-android-device-theme-is-it-dark-or-light – Nik Dec 19 '19 at 12:01
  • @Nik No, I know, how to detect the theme (night or dark). I need an answer how to detekt OS possibility for dark theme – Alexey Nikitin Dec 19 '19 at 17:30

1 Answers1

0

yes you can check this by below code

  switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
       case Configuration.UI_MODE_NIGHT_YES:
           Log.v("BaseActivity","THEME_DARK");
           break;
       case Configuration.UI_MODE_NIGHT_NO:
          Log.v("BaseActivity","THEME_LIGHT");
          break;

Edit

i have added theam as below by using below code.

  switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
        case Configuration.UI_MODE_NIGHT_YES:
            Log.v("BaseActivity","THEME_DARK");
            setTheme(R.style.AppTheme_Dark);
            Consts.currentTheme=Consts.THEME_DARK;
            break;
        case Configuration.UI_MODE_NIGHT_NO:
            Log.v("BaseActivity","THEME_LIGHT");
            setTheme(R.style.AppTheme);
            Consts.currentTheme=Consts.THEME_LIGHT;
            break;
YuvrajsinhJadeja
  • 1,383
  • 7
  • 23
  • No no, I know it, but I need to know about OS dark theme possibility. For example for Android 10 it is available by default, some devices with Android 9 accept it too, but all devices with android kitkat can not setup dark theme on all OS UI – Alexey Nikitin Dec 19 '19 at 11:59