Skip to content

Commit

Permalink
Merge pull request #812 from wbrawner/master
Browse files Browse the repository at this point in the history
Use system light/dark theme setting for Android 10+
  • Loading branch information
David-Development authored Jan 11, 2020
2 parents 4748792 + b49c230 commit c58de3a
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;
import android.util.Log;

import androidx.appcompat.app.AppCompatDelegate;
Expand All @@ -50,33 +51,36 @@ public enum THEME { LIGHT, DARK, OLED }
private ThemeChooser() { }

public static void chooseTheme(Activity act) {
int defaultNightMode;
switch(getSelectedThemeFromPreferences(false)) {
case 0: // Auto (Light / Dark)
Log.v(TAG, "Auto (Light / Dark)");
act.setTheme(R.style.AppTheme);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_TIME);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
defaultNightMode = AppCompatDelegate.MODE_NIGHT_AUTO_TIME;
} else { // Android 10+ (Q) supports a system-wide dark mode
defaultNightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
}
mSelectedTheme = THEME.LIGHT;
break;
case 1: // Light Theme
Log.v(TAG, "Light");
act.setTheme(R.style.AppTheme);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
defaultNightMode = AppCompatDelegate.MODE_NIGHT_NO;
mSelectedTheme = THEME.LIGHT;
break;
case 2: // Dark Theme
Log.v(TAG, "Dark");
act.setTheme(R.style.AppTheme);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
defaultNightMode = AppCompatDelegate.MODE_NIGHT_YES;
mSelectedTheme = THEME.DARK;
break;
default:
// This should never happen - just in case.. use the light theme..
Log.v(TAG, "Default");
act.setTheme(R.style.AppTheme);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_TIME);
defaultNightMode = AppCompatDelegate.MODE_NIGHT_AUTO_TIME;
mSelectedTheme = THEME.LIGHT;
break;
}
act.setTheme(R.style.AppTheme);
AppCompatDelegate.setDefaultNightMode(defaultNightMode);
}

public static void afterOnCreate(Activity act) {
Expand Down

0 comments on commit c58de3a

Please sign in to comment.