Make it Simple
Make it Simple

Reputation: 1882

How can I uncheck or reset the radio button?

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

Upvotes: 34

Views: 130105

Answers (9)

Lance Samaria
Lance Samaria

Reputation: 19572

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

Upvotes: 0

Pawan Harariya
Pawan Harariya

Reputation: 1394

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

Upvotes: 0

Vinay Revankar
Vinay Revankar

Reputation: 832

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

Upvotes: 1

Raul Cervera
Raul Cervera

Reputation: 51

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.

Upvotes: 5

Arjun Vekariya
Arjun Vekariya

Reputation: 976

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.

Upvotes: 23

Prateek
Prateek

Reputation: 61

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

Upvotes: 3

Haqq Nuru
Haqq Nuru

Reputation: 21

Put all in a group, then use:

buttonGroup1.clearSelection();

Upvotes: 2

user2823274
user2823274

Reputation: 51

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.

Upvotes: 3

Alexander
Alexander

Reputation: 48252

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

Upvotes: 78

Related Questions