Last active
November 12, 2020 05:33
-
-
Save HeyLookItsBrandon/9693410 to your computer and use it in GitHub Desktop.
Toggleable Android RadioButton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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