samedi 3 mars 2018

How to uncheck a radio button in radiogroup and listen to the action

I have 3 custom radio buttons and they're not checked by default. They act like a filter. I want to know if there's a way to uncheck a checked button and listen to that action. So basically I can cancel the filter.

I know I should use checkbox in this case, but I don't want more than one to be checked!!

 RadioGroup radioGroup;
 RadioButton radioYellow;
 RadioButton radioGreen;
 RadioButton radioRed;
 boolean isChecked;

void handleRadioButtons(){

    radioRed.setChecked(false);
    radioGreen.setChecked(false);
    radioYellow.setChecked(false);
    showAllCards();

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
       // RadioButton rad = group.findViewById(checkedId);
        Log.i("isChecked: ", isChecked + "");

       //I want to call showAllCards() when no button is checked

        switch (checkedId) {
          case R.id.radioRed:
            showRedCards();
            break;

          case R.id.radioGreen:
            showGreenCards();
            break;

          case R.id.radioYellow:
            showYellowCards();
            break;
        }
      }
    });

}




Aucun commentaire:

Enregistrer un commentaire