Skip to content

Instantly share code, notes, and snippets.

@HeyLookItsBrandon
Last active November 12, 2020 05:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save HeyLookItsBrandon/9693410 to your computer and use it in GitHub Desktop.
Save HeyLookItsBrandon/9693410 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