• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Themes Dark Mode in Shared preferences

Rubinia

Lurker
I made a dark mode in the settings of my apps and it worked out perfectly BUT I can't save it in SharedPreferences so it will stay how I choose it. I can do a background color in my SharedPreferences but this setting always causes Errors. What do I wrong? I hope someone can help me make it work. Thank you so much. When I run the code like I have it, the App won't even run. When I remove the SharedPreferences Part it works but it doesn't save. Hope someone can help me. I work on it since so many hours and cant solve it, because it is my first app.

here is the code:

Code:
public class Settings extends AppCompatActivity {

    private static final String TAG = "SettingsActivity";
    private RelativeLayout layout;
    private SharedPreferences preferences;
    private Bundle savedInstanceState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        assert getSupportActionBar() != null;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Spinner spinner = findViewById(DarkMode);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.e(TAG, "onItemSelected: " + position);
                handleNightMode(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Log.e(TAG, "onNothingSelected: ");
            }
        });

    }

    private void handleNightMode(int position) {

        switch (position) {
            case 0:
                Log.e(TAG, "Nothing Selected");
                break;
            case 1:
                Log.e(TAG, "FOLLLOW_SYSTEM");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                getDelegate().applyDayNight();
                break;
            case 2:
                Log.e(TAG, "YES");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                getDelegate().applyDayNight();
                break;
            case 3:
                Log.e(TAG, "NO");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                getDelegate().applyDayNight();
                break;
            case 4:
                Log.e(TAG, "AUTO");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
                getDelegate().applyDayNight();
                break;
            default:
                Log.e(TAG, "FOLLLOW_SYSTEM");
                break;
        }

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String NightMode = preferences.getString("prefTheme", "NO");
        if (NightMode.equals("YES"))
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        if (NightMode.equals("NO"))
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
        else if (NightMode.equals("AUTO"))
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
 
Back
Top Bottom