how can i uncheck or reset the radio button?

Make it Simple picture Make it Simple · Apr 11, 2013 · Viewed 93.6k times · Source

I'm new to android domain..

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 able to uncheck..

Answer

Alexander Kulyakhtin picture Alexander Kulyakhtin · Apr 11, 2013

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