How to uncheck RadioButton if already checked in RadioGroup

10,940

Solution 1

RadioGroup is mean for selecting one of two option, you should not disturb regular behavior of views. User compulsorily choice one of option from two. consider gender as radiogroup , there may be two or three option you have to choice one of them compulsorily. This can be achieve by simply adding below code in xml

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    app:layout_constraintLeft_toLeftOf="parent">

    <RadioButton
      android:id="@+id/radioButton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="RadioButton"
      tools:layout_editor_absoluteX="126dp"
      tools:layout_editor_absoluteY="83dp"/>

    <RadioButton
      android:id="@+id/radioButton2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="RadioButton"
      tools:layout_editor_absoluteX="172dp"
      tools:layout_editor_absoluteY="127dp"/>
  </RadioGroup>

and in activity class

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
            // you can check if radiobutton is checked or unchecked here , do needful codes
        }
    });

Solution 2

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().

Solution 3

I had just one radio button and I really didn't want to make a radio button group. The onClick and if and else check was not working to manually check and uncheck it. I read a couple of posts with onRadioButtonClick listeners etc.

First I tried to swap to checkbox-> same problem. Then I tried the CheckedTextView and what do you know, it actually worked like a toggle. So basically I added a radio button then left the text empty, added the checkedTextView next to it (overlapping) then linked the onClick function of the checkedTextView to a toggle function ->

public void toggle(View view)
{ 
    if(checkedTextView.isChecked())
    { 
        checkedTextView.setChecked(false); 
        actualRadioBtn.setChecked(false);
    }
    else
    {
        checkedTextView.setChecked(true);
        actualRadioBtn.setChecked(true);
    }
}

Then in your function where you check if that radio button is selected you can simply use

if(actualRadioBtn.isChecked())....
Share:
10,940
Tushar Kotecha
Author by

Tushar Kotecha

Updated on June 10, 2022

Comments

  • Tushar Kotecha
    Tushar Kotecha almost 2 years

    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>
    
  • IgorGanapolsky
    IgorGanapolsky about 4 years
    Have you looked at Kotlin?
  • benc
    benc almost 4 years
    Can you provide more details how why CheckBox is a better choice? TIA.
  • Paul Addai
    Paul Addai almost 4 years
    Yeah sure. You can actually uncheck checkbox by default.