Android - ToggleButton setChecked and setSelected not working

android, sharedpreferences, state, togglebutton

Solution

Instead of `==` use `.equals()` for string comparison..

Try this,

if (volReturned.equals("On")){
        /*tbtnvol.setChecked(true);*/
        tbtnvol.setSelected(boolT);
    }else{
        tbtnvol.setSelected(boolF);
        }
    if (vibReturned.equals("On")){
        tbtnvib.setSelected(boolT);
    }else{
        tbtnvib.setSelected(boolF);
        }

Problem

What I am trying to do is checking the `SharedPreferences` if the volume and vibration is `on` or `off`. If it is on the `ToggleButton` should be set to `on`, else, `off`. I already tried using `setChecked()` and `setSelected()` on `ToggleButton`s but it didn't change the `TogggleButton`s' state. I also tried initializing a variable for the `true` and `false` just to make sure but still doesn't work. What seems to be the problem? ``` optionsDB = getSharedPreferences(table, 0); String volReturned = optionsDB.getString("volume", "Couldn't load data"); String vibReturned = optionsDB.getString("vibration", "Couldn't load data"); Toast.makeText(this, "Vol: "+volReturned+" Vib: "+ vibReturned, Toast.LENGTH_LONG).show(); boolean boolT = true; boolean boolF = false; if (volReturned=="On"){ /*tbtnvol.setChecked(true);*/ tbtnvol.setSelected(boolT); }else{ tbtnvol.setSelected(boolF); } if (vibReturned=="On"){ tbtnvib.setSelected(boolT); }else{ tbtnvib.setSelected(boolF); } ```

Original source