0

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;
}
VISCREO
  • 19
  • 11
  • 1
    In the Settings activity you save the switch state to SharedPrefences and in other activities you read it from SharedPreferences as in [How to use SharedPreferences in Android to store, fetch and edit values](https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values). – Markus Kauppinen Jan 17 '19 at 10:28
  • @MarkusKauppinen that link was actually super helpful for getting an understanding of how it all works thanks! But how would I get it working with Boolean? I checked some of the answers further down but non of them worked for me :( Am I being dumb with this? I've also added the "SharedPref" file. – VISCREO Jan 17 '19 at 16:19
  • @MarkusKauppinen can you put this as an answer so that I can mark it as correct? It didn't work but it put it all into perspective for me and thanks to that I figured it out! – VISCREO Jan 18 '19 at 11:24

2 Answers2

1

In you application class inside onCreate

SharedPreferences sharedPreferences = getSharedPreferences("Your_Shared_pref", Context.MODE_PRIVATE);
boolean nightMode = sharedPreferences.getBoolean(SettingsActivity.DARK_THEME_PREFERENCE_KEY, false);

AppCompatDelegate.setDefaultNightMode(nightMode ? MODE_NIGHT_YES : MODE_NIGHT_NO);

Than in your activity, do this:

 switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        AppCompatDelegate.setDefaultNightMode(isChecked ? MODE_NIGHT_YES : MODE_NIGHT_NO);
        if (isChecked) {
           sharedPref.setNightModeState(true);
            recreate();
        } else {
            sharedPref.setNightModeState(false);
            recreate();
        }
    }
});

@Override
public void recreate() {
    finish();
    overridePendingTransition(R.anim.anime_fade_in,
            R.anim.anime_fade_out);
    startActivity(getIntent());
    overridePendingTransition(R.anim.anime_fade_in,
            R.anim.anime_fade_out);
}

you can find the animation xml online.

Rahul
  • 4,699
  • 5
  • 26
  • 38
  • Unfortunately that didn't work but still managed to get it working. I'll post what I did in an answer below. I love the idea of an animation though so I'll up vote your answer once I get enough reputation to do so! – VISCREO Jan 18 '19 at 11:25
0

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);
VISCREO
  • 19
  • 11