Make it Simple
Make it Simple

Reputation: 1882

How to uncheck the radio buttons in group?

I'm doing a quiz app I have a radio group with 4 radio buttons and I have one next button.

While displaying First question and answers its no problem in check..when user gives next, the radio group should uncheck. How to implement this?

when user select the option it should verify the answer is correct or incorrect. I have stored the correct answers in a array..When user toggle the options it shows error in toggle buttons. How to implement this?

   btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) { 
                RadioButton radioButton = (RadioButton)group. findViewById(checkedId);
                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;
                }
                crrtans=new ArrayList<String>(new ArrayList<String>(crrtans));
                if (temp.equals(crrtans.get(l))){
                TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                txtRadio.setText("" + radioButton.getText() + " IS CORRECT");   
                txtRadio.setTextColor(Color.parseColor("#008000"));
               }else{
              TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
          txtRadio.setText("" + radioButton.getText() + "is INCORRECT");
          txtRadio.setTextColor(Color.parseColor("#FF0000")); 
              }
            }
     });
    Button nextBtn = (Button) findViewById(R.id.nxt_btn);
    nextBtn.setOnClickListener(new Button.OnClickListener(){
    public void onClick(View v){
         if (j == ques1.size() -1) {
                showAlert1();
            }
          else{ 
            ++j;
            ++num;
            TextView txtque = (TextView) findViewById(R.id.que_txt); 
            txtque.setText("Q" + num +ques1.get(j));
                        }      
         });

Upvotes: 0

Views: 2459

Answers (2)

Munim
Munim

Reputation: 2778

The problem is inside your public void onCheckedChanged(RadioGroup group, int checkedId) you are creating an instance of the checked radio button based on checkedId which can be null once you call clearCheck. The fact is once you call clearCheck on your radio group when the next button is clicked,the onCheckedChanged event gets fired and it gets a null checkedId. As a result you get a null radio button when you do this: RadioButton radioButton = (RadioButton)group. findViewById(checkedId);

EDIT(Better explanation):

Add this method to your class:

private void doProcessingWithRadioButton(int checkedId)
    {
        RadioButton radioButton = (RadioButton)group. findViewById(checkedId);
        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;
        }
        crrtans=new ArrayList<String>(new ArrayList<String>(crrtans));
        if (temp.equals(crrtans.get(l))){
            TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
            txtRadio.setText("" + radioButton.getText() + " IS CORRECT");   
            txtRadio.setTextColor(Color.parseColor("#008000"));
       }else{
          TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
          txtRadio.setText("" + radioButton.getText() + "is INCORRECT");
          txtRadio.setTextColor(Color.parseColor("#FF0000")); 
       }
    }

Now change your code like this:

btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) { 
            if(checkedId != -1)
            {
                doProcessingWithRadioButton(checkedId);
            }
        });

Hope this helped. Please don't forget to mark this as answer if it solved your problem.

Upvotes: 1

saran
saran

Reputation: 461

To clear all radio buttons, that is none of the radio buttons in the group to be checked use

clearCheck() 

for example ur

radiogroupname.clearCheck();

Upvotes: 0

Related Questions