16

i recently integrated butterknife in my android project, and now i am trying to use @OnCheckedChanged annotation for radiogroup. but getting error of not giving callback. So what is the right method to call and get checkedId or this one is for radiobutton only and not for radiogroup.

@OnCheckedChanged(R.id.gendergroupid)
void onGenderSelected(RadioGroup group, int checkedId){
    switch(checkedId){
        case R.id.maleid:
            maleid.setEnabled(true);
            maleid.setChecked(true);
            break;
        case R.id.femaleid:
            femaleid.setEnabled(true);
            femaleid.setChecked(true);
            break;
        case R.id.bothid:
            bothid.setEnabled(true);
            bothid.setChecked(true);
            break;
    }
}

Gives me error

BloError:(89, 10) error: Unable to match @OnCheckedChanged method arguments.

Parameter #1: android.widget.RadioGroup did not match any listener parameters

Parameter #2: int did not match any listener parameters

Methods may have up to 2 parameter(s):

android.widget.CompoundButton boolean

These may be listed in any order but will be searched for from top to bottom.ckquote

Niki
  • 1,566
  • 1
  • 19
  • 36

2 Answers2

53

According the specification, this annotation needs to be used with 2 parameters, a CompoundButton and a boolean, so if you really want to use this listener, you have to change it like this:

@OnCheckedChanged(R.id.gendergroupid)
void onGenderSelected(CompoundButton button, boolean checked) {
   //do your stuff.
}

I think in your case this listener doesn't work, so you can use another implementation like:

@OnClick({R.id.radio_1, R.id.radio_2}) 
public void onRadioButtonClicked(RadioButton radioButton) {
    // Is the button now checked?
    boolean checked = radioButton.isChecked();

    // Check which radio button was clicked
    switch (radioButton.getId()) {
      case R.id.radio_1:
        if (checked) {
          // 1 clicked
        }
        break;
      case R.id.radio_2:
        if (checked) {
          // 2 clicked
        }
        break;
    }
}
jobbert
  • 3,297
  • 27
  • 43
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42
  • 3
    Trying to use the first approach: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.CompoundButton – Someone Somewhere Feb 21 '18 at 22:29
  • A CompoundButton is a single button, not a group like RadioGroup. – Luiz Fernando Salvaterra Feb 22 '18 at 11:02
  • RadioGroup extends LinearLayout, if you use @OnCheckedChange butterknife tryes to cast "RadioGroup" to "CompoundButton". This works only for "RadioButton" so you need to use a list of the radio buttons ids inside the radio group instead of the radio group itself. – Z3R0 Nov 12 '19 at 09:00
17

this worked for me

@OnCheckedChanged({R.id.radio_button1, R.id.radio_button2})
public void onRadioButtonCheckChanged(CompoundButton button, boolean checked) {
        if(checked) {
            switch (button.getId()) {
                case R.id.radio_button1:
                    // do stuff
                    break;
                case R.id.radio_button2:
                    // do stuff
                    break;
            }
        }
    }
Seph Remotigue
  • 427
  • 1
  • 3
  • 9
  • 1
    @vuhung3990 you may have to remove the onClick implementation so it will trigger only once – Seph Remotigue Aug 30 '17 at 08:05
  • hi @Seph Remotigue, this is my mistake if radiogroup have 2 radiobutton, it will trigger 1 for notify button 1 check, 1 for notify button 2 uncheck but you have a conditional `checked` will only trigger 1 time, you are right – vuhung3990 Aug 30 '17 at 09:01