Android Remember Dark Theme

297 Views Asked by At

I'm trying to create a dark theme for my app. So far so good, but when I close the app, the app does not remember the dark theme that was set.

I change the dark theme with the following method:

    public static void setDarkMode(Object value, SharedPreferences preferences) {
        SharedPreferences.Editor editor = preferences.edit();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            if (value.equals("on")) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                editor.putString("dark_theme", (String) value);
                editor.apply();
            } else if (value.equals("off")) {
                Log.i(TAG, "off");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                editor.putString("dark_theme", (String) value);
                editor.apply();
            } else if (value.equals("follow_system")) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                editor.putString("dark_theme", (String) value);
                editor.apply();
            } else {
                Log.e(TAG, "Dark Mode Preferences android Q+ did not give the right value");
            }
        } else {
            if ((boolean) value) {
                Log.i(TAG, "Set night mode on");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                editor.putBoolean("dark_theme", (boolean) value);
                editor.apply();
            } else {
                Log.i(TAG, "set night mode off");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                editor.putBoolean("dark_theme", (boolean) value);
                editor.apply();
            }
        }
    }

And in the MainActivity, I try to set the mode by calling the method setDayNightMode() but this always sets MODE_NIGHT_FOLLOW_SYSTEM.

    private void setDayNightMode() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            Log.i(TAG, "Build version >= Q");
            String setting = settings.getString("dark_theme", "follow_system");
            if (setting.equals(getResources().getString(R.string.on))) {
                Log.i(TAG, "night mode on");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            } else if (setting.equals(getResources().getString(R.string.off))) {
                Log.i(TAG, "Night mode off");
                AppCompatDelegate.setDefaultNightMode((AppCompatDelegate.MODE_NIGHT_NO));
            } else if (setting.equals(getResources().getString(R.string.follow_sys))) {
                Log.i(TAG, "Night mode follow system");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
            }
        } else {
            Boolean setting = settings.getBoolean("dark_theme", false);
            if(setting) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            } else if (!setting) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            }
        }
    }

It seems like the SharedPreferences settings which is defined by this.getSharedPreferences(getResources().getString(R.string.dark_theme_preference_key), MODE_PRIVATE) does allways return the follow_system value. Do you know how to remember the dark mode in the app?

1

There are 1 best solutions below

0
On

Make use of Shared Preferences when the user changes the theme of your app in the settings. Then when the user starts the app, on the main activity in onCreate, load the key value pair and set the theme according to the saved choice of the user