Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ToggleButton setChecked and setSelected not working

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 ToggleButtons but it didn't change the TogggleButtons' 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);
        }
like image 700
Harambe Attack Helicopter Avatar asked Sep 06 '25 06:09

Harambe Attack Helicopter


1 Answers

Well, there are two methods associated with ToggleView:

  1. setChecked(Boolean value)

  2. setSelected(Boolean value)

And both of them are to be used in pair. If you want to set toggle view selected, pass "true" to both these methods and vice-versa.

The reason being, setChecked() sets the intrinsic boolean dataMember associated with your view object and setSelected sets the UI associated with your view object.

Yes, this is wired and actually one method should set both UI and data, but this is how it was implemented and thus this is how we must use these methods-in pair.

like image 62
Parth Kapoor Avatar answered Sep 09 '25 00:09

Parth Kapoor