Change Android Switch State
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()
}
}
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");
}
});