javaandroidsharedpreferencesandroid-night-mode

How do I get my theme to programmatically get carried over into other activities?


I've created a function in the settings page of my app which contains a switch - that when pressed - switches to a secondary "Night" theme. I followed this tutorial for the most part. However, I don't know how to carry this night mode into my other activities? I've tried calling the "if switch checked" in my main activity, but it obviously doesn't see that switch. What I mainly need to know is, how do I check a switch state in another activity? And is this even the right way of doing it? Let me know if I've missed anything else in this question.

// ======== CODE FOR THE SETTINGS PAGE ======== //

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


    // ======== Night Mode ======== //
    SwitchCompat switchCompat;
    final SharedPref sharedPref;

    sharedPref = new SharedPref(this);

    if (sharedPref.loadNightModeState()) {
        setTheme(R.style.AppTheme_Night);
        getSupportActionBar().setBackgroundDrawable(getDrawable(R.drawable.actionbar));
        actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.BackgroundLight));
    } else setTheme(R.style.AppTheme);

    setContentView(R.layout.activity_settings);
    switchCompat = (SwitchCompat) findViewById(R.id.night_switch);
    if (sharedPref.loadNightModeState()) {
        switchCompat.setChecked(true);
    }

    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
               sharedPref.setNightModeState(true);
                restartApp();
            } else {
                sharedPref.setNightModeState(false);
                restartApp();
            }
        }
    });
}




private void restartApp() {
    Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
    startActivity(intent);
    finish();
}

// ======== SharedPref CODE ======== //

public class SharedPref {

private SharedPreferences sharedPreferences;


public SharedPref(Context context) {
    sharedPreferences = context.getSharedPreferences("filename", Context.MODE_PRIVATE);
}


public void setNightModeState(Boolean state) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("NightMode",state);
    editor.apply();
}

public Boolean loadNightModeState (){
    Boolean state = sharedPreferences.getBoolean("NightMode", false);
    return state;
}

Solution

  • I put this between my onCreate and my super.onCreate. The code I used was taken from my settings java page. It turns out that it was pretty easy to figure, I just needed someone to put it into perspective!

     @Override
        protected void onCreate(Bundle savedInstanceState) {
    
    
            final SharedPref sharedPref;
            sharedPref = new SharedPref(this);
    
            if (sharedPref.loadNightModeState()) {
                setTheme(R.style.AppTheme_Night);
                //restartApp();
                //getSupportActionBar().setBackgroundDrawable(getDrawable(R.drawable.actionbar));
                //actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.BackgroundLight));
            } else setTheme(R.style.AppTheme);
            //restartApp();
    
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);