Android RdioGroup clearCheck conflict with setOnCheckedChangeListener

 * <p>Interface definition for a callback to be invoked when the checked
 * radio button changed in this group.</p>
public interface OnCheckedChangeListener {
     * <p>Called when the checked radio button has changed. When the
     * selection is cleared, checkedId is -1.</p>
     * @param group the group in which the checked radio button has changed
     * @param checkedId the unique identifier of the newly checked radio button
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId);

When you call clearCheck() method on the RadioGroup, if there is a RadioButton is checked, it will set the state of this view to unchecked, then notify the app about the state of a radio button changed by calling onCheckedChanged() method of the instance that you pass into setOnCheckedChangeListener() method.

This explains why in your app, if users select a wrong answer, it will call onCheckedChange() twice. The former is when the state change from unchecked to checked, the latter is when the state change from checked to unchecked via clearCheck(). This is the expected behavior.

Solution: Clear the OnCheckedChangeListener before calling clearCheck() method, then set it after calling clearCheck() method.

// Declare this variable
private val onCheckedChangeListener = object : RadioGroup.OnCheckedChangeListener {
    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        if (checkedId != -1) {

// Calling this line once, inside onCreate() of Activity 
// or onCreateView() of Fragment for example

private fun checkAnswer(checkedId: Int) {
    // Your logic to check answer here
    // ...

    // If the answer is wrong, just clear check

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top