@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_about, menu); MenuItem dayNight = menu.findItem(R.id.menu_night_mode); dayNight.setChecked(AppCompatDelegate.getDefaultNightMode() == MODE_NIGHT_YES); return super.onCreateOptionsMenu(menu); }
@Override protected void onCreate(Bundle savedInstanceState) { ThemeManager helper = ThemeManager.getInstance(this); helper.addThemeChangeListener(this); mDarkMode = AppCompatDelegate.getDefaultNightMode(); super.onCreate(savedInstanceState); }
@Override public void onConfigurationChanged(Configuration newConfig) { if (AppCompatDelegate.getDefaultNightMode() == mDarkMode) { if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) { newConfig.uiMode &= ~(Configuration.UI_MODE_NIGHT_MASK); newConfig.uiMode |= Configuration.UI_MODE_NIGHT_YES; getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics()); } } ThemeManager helper = ThemeManager.getInstance(this); helper.applyThemeToActivity(this); super.onConfigurationChanged(newConfig); }
public static void restoreSavedState(Context context) { int savedNightModeState = loadNightModeState(context); int currentNightModeState = AppCompatDelegate.getDefaultNightMode(); if (savedNightModeState != currentNightModeState) { switch (savedNightModeState) { case AppCompatDelegate.MODE_NIGHT_NO: // turn on day mode activateDayMode(context, false); break; case AppCompatDelegate.MODE_NIGHT_YES: // turn on night mode activateNightMode(context, false); break; default: // turn on mode "follow system" activateFollowSystemMode(context, false); break; } } }
public static void switchMode(Activity activity) { // SWITCH: undefined -> night / night -> day / day - undefined switch (AppCompatDelegate.getDefaultNightMode()) { case AppCompatDelegate.MODE_NIGHT_NO: // currently: day mode -> switch to: follow system displayDefaultStatusBar(activity); // necessary hack :-/ activateFollowSystemMode(activity, true); break; case AppCompatDelegate.MODE_NIGHT_YES: // currently: night mode -> switch to: day mode displayLightStatusBar(activity); // necessary hack :-/ activateDayMode(activity, true); break; default: // currently: follow system / undefined -> switch to: day mode displayLightStatusBar(activity); // necessary hack :-/ activateNightMode(activity, true); break; } }