+ // Store the app theme entry number.
+ appThemeEntryNumber = 2;
+ } else { // The system default theme is selected.
+ // Store the app theme entry number.
+ appThemeEntryNumber = 0;
+ }
+
+ // 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.
+ switch (appThemeEntryNumber) {
+ case 0: // 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);
+ }
+ break;
+
+ case 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);
+ break;
+
+ case 2:
+ // Update the theme preference summary text.
+ appThemePreference.setSummary(appThemeEntriesStringArray[2]);
+
+ // Apply the new theme.
+ AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
+ break;
+ }
+
+ // Disable the WebView theme preference if the API >= 33 and the app theme is set to light.
+ webViewThemePreference.setEnabled((Build.VERSION.SDK_INT < 33) || (appThemeEntryNumber != 1));