- case "dark_theme":
- // Update the icon.
- if (sharedPreferences.getBoolean("dark_theme", false)) {
- darkThemePreference.setIcon(R.drawable.theme_dark);
- } else {
- darkThemePreference.setIcon(R.drawable.theme_light);
+ case "app_theme":
+ // Get the new theme.
+ String newAppTheme = sharedPreferences.getString("app_theme", getString(R.string.app_theme_default_value));
+
+ // Update the system according to the new theme. A switch statement cannot be used because the theme entry values string array is not a compile time constant.
+ if (newAppTheme.equals(appThemeEntryValuesStringArray[1])) { // The light theme is selected.
+ // Update the theme preference summary text.
+ appThemePreference.setSummary(appThemeEntriesStringArray[1]);
+
+ // Apply the new theme.
+ AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
+ } else if (newAppTheme.equals(appThemeEntryValuesStringArray[2])) { // The dark theme is selected.
+ // Update the theme preference summary text.
+ appThemePreference.setSummary(appThemeEntriesStringArray[2]);
+
+ // Apply the new theme.
+ AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
+ } else { // The system default theme is selected.
+ // Update the theme preference summary text.
+ appThemePreference.setSummary(appThemeEntriesStringArray[0]);
+
+ // Apply the new theme.
+ if (Build.VERSION.SDK_INT >= 28) { // The system default theme is supported.
+ // Follow the system default theme.
+ AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
+ } else {// The system default theme is not supported.
+ // Follow the battery saver mode.
+ AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
+ }