user2677095
user2677095

Reputation: 471

Change Android Switch State

I currently am developing an app that has a menu, and one of the options on the menu is "Settings" where the user can basically decide to turn off sounds and other things like that. I currently have two switches in the Settings activity. Here is the java code for the Settings activity so far:

import android.support.v7.app.ActionBarActivity;


public class Options extends ActionBarActivity {
private  Switch ding;
private Switch countdown;
public  boolean isDingChecked;
public  boolean isCountdownChecked;
public static final String PREFS = "examplePrefs";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
ding = (Switch) findViewById(R.id.switch1);
ding.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        SharedPreferences examplePrefs = getSharedPreferences(PREFS,0);
        Editor editor = examplePrefs.edit();
        editor.putBoolean("userMessage", isChecked);
        editor.commit();

        //System.out.println(examplePrefs.getBoolean("userMessage", isChecked));
        isDingChecked = examplePrefs.getBoolean("userMessage", isChecked);
        System.out.println(isDingChecked + " is ding checked");
        ding.setChecked(isDingChecked);
    }
});

countdown = (Switch) findViewById(R.id.switch2);
countdown.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // do something, the isChecked will be
        // true if the switch is in the On position
        isCountdownChecked = isChecked;

    }
});     
 }
 }

I am able to use the boolean values in my other activity, so the SharedPreference works fine. However, when I go back to my menu activity and go back to this options activity, the state of the switches goes back to its default values of being true, regardless of what the user states. Is there anyway I can fix this?

ding.setChecked(isDingChecked)

Isn't really doing anything I guess. I know I posted a question similar to this in the past, it's just that one hasn't had much activity and I've been working on this issue for quite some time. Thanks!

Upvotes: 14

Views: 50331

Answers (3)

jobbert
jobbert

Reputation: 3497

Of course the Kotlin answer:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_options)
    val examplePrefs = getSharedPreferences(PREFS, 0)
    val editor = examplePrefs.edit()
    ding.setChecked(examplePrefs.getBoolean("your_key", false))
    
    ding.setOnCheckedChangeListener{_, checked ->
        examplePrefs.putBoolean("your_key", checked).apply()
    }
}

Upvotes: 1

Will Thomson
Will Thomson

Reputation: 885

Try something like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_options);
    ding = (Switch) findViewById(R.id.switch1);

    //grab prefs first
    final SharedPreferences examplePrefs = getSharedPreferences(PREFS,0);
    final Editor editor = examplePrefs.edit();
    ding.setChecked(examplePrefs.getBoolean("your_key", false)); //false default 


    ding.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //commit prefs on change
            editor.putBoolean("your_key", isChecked);
            editor.commit();

            System.out.println(isDingChecked + " is ding checked");
        }
    });

Upvotes: 26

reVerse
reVerse

Reputation: 35264

The setChecked(value) method of the Switch works totally fine but you're calling it inside the onCheckedChanged(...) method which is unnecessary.
So in order to set the Switch to the latest value you should load the Preferences and set the checked state outside of the setOnCheckedChangeListener listener.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_options);
    // your current code

    //load and set preferences
    SharedPreferences examplePrefs = getSharedPreferences(PREFS,0);
    isDingChecked = examplePrefs.getBoolean("userMessage", isChecked);
    ding.setChecked(isDingChecked);
}

Upvotes: 0

Related Questions