Search code examples
androidradio-buttonradio-group

How to uncheck RadioButton if already checked in RadioGroup


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>

Solution

  • 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
            }
        });