Android – How to uncheck RadioButton if already checked in RadioGroup

androidradio-buttonradio-group

I have two RadioButtons in RadioGroup, initially both are unchecked. I want to uncheck RadioButton if it is already checked. I have tried but I'm unable to do it. When I choose any radio button then it is not changing state to checked first time, if I select same radio button second time then it is changing its state, and if I select other radio button if one button is checked then it is also not changing the state first time and all button get unchecked can anyone help me? My code is given below…

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
pus="";

boolean isChecked = checkedRadioButton.isChecked();
 if(isChecked){
     checkedRadioButton.setChecked(false);

 }else{
     checkedRadioButton.setChecked(true);

 }
}
 Toast.makeText(context, pus, Toast.LENGTH_SHORT).show();
}
});
<RadioGroup
    android:id="@+id/rdbt_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"

    android:gravity="center"
    android:orientation="horizontal" >

    <RadioButton
    android:id="@+id/radio_no"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/rbtn_selector"
    android:button="@null"
    android:gravity="center"
    android:paddingBottom="5dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    android:clickable="true"
    android:textColor="@drawable/rbtn_textcolor_selector"
    android:textSize="15sp" />

    <RadioButton
    android:id="@+id/radio_yes"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:clickable="true"
    android:textSize="15sp"
    android:background="@drawable/rbtn_selector"
    android:textColor="@drawable/rbtn_textcolor_selector"
    android:padding="5dp"
    android:gravity="center"
    />
</RadioGroup>

Best Answer

I know it's a bit late to answer this question but still, if it can help someone else I would be glad.

Just make a custom class extending RadioButton or AppCompatRadioButton and override toggle() method.

public class UncheckableRadioButton extends AppCompatRadioButton {

    public UncheckableRadioButton(Context context) {
        super(context);
    }

    public UncheckableRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public UncheckableRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void toggle() {

        if (isChecked()) {
            if (getParent() != null && getParent() instanceof RadioGroup) {
                ((RadioGroup) getParent()).clearCheck();
            }
        } else {
            super.toggle();
        }
    }

    @Override
    public CharSequence getAccessibilityClassName() {
        return UncheckableRadioButton.class.getName();
    }
}

And this will also work if you check radioGroup.getCheckedRadioButtonId().