How can I uncheck or reset the radio button?
Asked Answered
M

9

34

I am developing an quiz based app. There will be 1 question and 4 option (radio buttons) when user opens this app radio button will be unchecked but the problem comes when the user answers 1 quest and when he goes for next quest radio button will be checked. I want to uncheck/reset the radio buttons for every question. How can I do it?

  answ1=new ArrayList<String>(new ArrayList<String>(answ1));
        btn_practice1.setText(answ1.get(0));
        btn_practice2.setText(answ1.get(1));
        btn_practice3.setText(answ1.get(2));
        btn_practice4.setText(answ1.get(3));
        btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {             
                RadioButton radioButton = (RadioButton)group. findViewById(checkedId); //line 262
                String temp = radioButton.getText().toString();
                switch(btn_practicerg.getCheckedRadioButtonId()){
                case R.id.RB1:
                    if (btn_practice1.isChecked()){
                        btn_practice2.setChecked(false);
                        btn_practice3.setChecked(false);
                        btn_practice4.setChecked(false);
                    }
                   break;
                case R.id.RB2:
                    if (btn_practice2.isChecked()){
                        btn_practice1.setChecked(false);
                        btn_practice3.setChecked(false);
                        btn_practice4.setChecked(false);
                    }                    
                    break;
                case R.id.RB3:
                    if (btn_practice3.isChecked()){
                        btn_practice1.setChecked(false);
                        btn_practice2.setChecked(false);
                        btn_practice4.setChecked(false);
                    }
                    break;
                case R.id.RB4:
                    if (btn_practice4.isChecked()){
                        btn_practice1.setChecked(false);
                        btn_practice2.setChecked(false);
                        btn_practice3.setChecked(false);
                    }                   
                    break;
                default:
                    btn_practice1.setChecked(false);
                    btn_practice2.setChecked(false);
                    btn_practice3.setChecked(false);
                    btn_practice4.setChecked(false);
                }  
        ImageView nextBtn = (ImageView) findViewById(R.id.nxt_btn);
    nextBtn.setOnClickListener(new Button.OnClickListener(){
    public void onClick(View v){
         btn_practicerg.clearCheck();  //line 355
              }
             });

Logcat

     E/AndroidRuntime(729):at    
      com.example.TEENEINSTIEN.Question$LoadQuestions$2.onCheckedChanged(Question.java:262)
      E/AndroidRuntime(729):at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
      E/AndroidRuntime(729):at android.widget.RadioGroup.check(RadioGroup.java:166)
      E/AndroidRuntime(729):at android.widget.RadioGroup.clearCheck(RadioGroup.java:205)
      E/AndroidRuntime(729):at   
      com.example.TEENEINSTIEN.Question$LoadQuestions$3.onClick(Question.java:355)
      E/AndroidRuntime(729):at android.view.View.performClick(View.java:2485)
      E/AndroidRuntime(729):at android.view.View$PerformClick.run(View.java:9080)

                 

I try like this, but I can't uncheck..

Mcgowen answered 11/4, 2013 at 9:30 Comment(3)
I think really you need to use RadioGroup if so check the links developer.android.com/reference/android/widget/RadioGroup.html #6781481Hibachi
First of, you should consider using a RadioGroup to avoid having to "uncheck" them manually.Trinitytrinket
RadioGroup only i have used, btn_practicerg its only radio group in my code...Mcgowen
M
23

I think the problem is with btn_practicerg object i.e if you are creating new RadioGroup object everytime you display a new question then you have to execute RadioGroup.clearCheck() on new btn_practicerg object instead of old one that I think you are doing currently.

Even better:

    btn_practice1.setText(answ1.get(0));
    btn_practice2.setText(answ1.get(1));
    btn_practice3.setText(answ1.get(2));
    btn_practice4.setText(answ1.get(3));
    btn_practice1.setChecked(false);
    btn_practice2.setChecked(false);
    btn_practice3.setChecked(false);
    btn_practice4.setChecked(false);

to uncheck all the buttons in the beginning. I hope this will solve your problem.

Macarthur answered 11/4, 2013 at 10:8 Comment(1)
Its uncheck, but one problem, in 1st question i select option1 means same option1 if select in 2nd quest its not marking or not selecting if other options i select its selecting...Mcgowen
A
78

Put all your buttons in a RadioGroup then when you need to clear them all use RadioGroup.clearCheck();

Atmosphere answered 11/4, 2013 at 9:35 Comment(3)
this should be the accepted answer as it reduces boilerplate code and improved readability.Sabinasabine
This gives null pointer exceptions while clearing the checked radio buttons of radio group.Bathelda
Note: clearCheck() calls onCheckedChanged twice. Check this out #4519603Stores
M
23

I think the problem is with btn_practicerg object i.e if you are creating new RadioGroup object everytime you display a new question then you have to execute RadioGroup.clearCheck() on new btn_practicerg object instead of old one that I think you are doing currently.

Even better:

    btn_practice1.setText(answ1.get(0));
    btn_practice2.setText(answ1.get(1));
    btn_practice3.setText(answ1.get(2));
    btn_practice4.setText(answ1.get(3));
    btn_practice1.setChecked(false);
    btn_practice2.setChecked(false);
    btn_practice3.setChecked(false);
    btn_practice4.setChecked(false);

to uncheck all the buttons in the beginning. I hope this will solve your problem.

Macarthur answered 11/4, 2013 at 10:8 Comment(1)
Its uncheck, but one problem, in 1st question i select option1 means same option1 if select in 2nd quest its not marking or not selecting if other options i select its selecting...Mcgowen
N
5

if the Radiobutton belong to a radiogroup you can NEVER unchecked alone that button programaticaly, so you must to do a method to unched all first.

    public void unchecked()
     {
     RadioGroup x=findViewById(R.id.NameOfRadiogroup);
     x.clearCheck();
     }  

and then you must to call the method.

Norge answered 4/11, 2018 at 15:18 Comment(0)
D
3

Try this one, the same problem got solved with this solution:

  1. set onclick method to all 4 radiobuttons say radio1Clicked, radio2Clicked & so on
  2. in respective onclick method add below code:

    public void radio1Clicked(View view)
    {
        // Note that I have unchecked  radiobuttons except the one
        // which is clicked/checked by user
        radio2Button.setChecked(false);
        radio3Button.setChecked(false);
        radio4Button.setChecked(false);
    }
    
  3. simillarly do for remaining radiobuttons. Hope this will solve your problem.

Please feel free to ask any queries regarding the solution.

Dandruff answered 7/10, 2013 at 9:26 Comment(0)
R
3

I had just one radio button and I really didn't want to make a radio button group. 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())....
Realtor answered 3/3, 2018 at 4:15 Comment(0)
K
2

Put all in a group, then use:

buttonGroup1.clearSelection();
Kiloliter answered 11/6, 2018 at 16:21 Comment(0)
H
1

Below is the code of single radio button

radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!radioButton.isSelected()) {
                    radioButton.setChecked(true);
                    radioButton.setSelected(true);
                } else {
                    radioButton.setChecked(false);
                    radioButton.setSelected(false);
                }
            }
        });
Humphries answered 14/12, 2021 at 6:20 Comment(0)
E
0

If you have a single radio button and you want to uncheck it on user click, once it was already checked use an extra boolean variable that tracks the checked state of the radio button.

        boolean check  = false;   // initial state of your radio button
        myRadioButton.setOnClickListener(view -> {
            if (check) {
                myRadioButton.setChecked(false);
                check = false;
                //write code for when button is unchecked
            } else {
                check = true;
                myRadioButton.setChecked(true);
                //write code for when button is checked
            }
        });
Emission answered 18/11, 2021 at 12:57 Comment(0)
P
0

I don't have my radio buttons in a group. To clear it from a specific button in Kotlin use:

binding.whicheverRadioButton.isChecked = false

Of course to check it do the opposite

binding.whicheverRadioButton.isChecked = true
Permatron answered 29/5, 2023 at 2:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.