Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save milaptank/8e1b624468858124ea350df3a2710699 to your computer and use it in GitHub Desktop.
Save milaptank/8e1b624468858124ea350df3a2710699 to your computer and use it in GitHub Desktop.
Toggleable Android RadioButton
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RadioButton;
import android.widget.RadioGroup;
/** Extension of Android's RadioButton that restores CompoundButton's check toggling
* behavior, allowing a checked RadioButton to be unchecked by tapping on it again. */
public class ToggleableRadioButton extends RadioButton {
public ToggleableRadioButton(Context context) {
super(context);
}
public ToggleableRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ToggleableRadioButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void toggle() {
if(isChecked()) {
if(getParent() instanceof RadioGroup) {
((RadioGroup)getParent()).clearCheck();
}
} else {
setChecked(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment