简体   繁体   中英

How to check if dark theme exists in Android OS?

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM