1

I would like to know if it possible to use the RadioGroup to set all RadioButtons to a specified value. In my case I have this:

public void displayFalse(View view) {

    if(view.getId() == R.id.btnPrimers)

        for (int i = 0; i < rgPrimer.getChildCount(); i++) {
            rgSegon.getChildAt(i).setEnabled(false);
            //something like: .setChecked(false);

    }else if(view.getId() == R.id.btnSegons){

        for (int i = 0; i < rgSegon.getChildCount(); i++) {
            rgSegon.getChildAt(i).setEnabled(false);
        }
    }else if(view.getId() == R.id.btnSegons) {

        for (int i = 0; i < rgPostre.getChildCount(); i++) {
            rgPostre.getChildAt(i).setEnabled(false);
        }
    } else {

    }

}

So I have different <LinearLayout> where each one is visible using a button. So if the button has been selected, the layout will appear and so will all the radioGroup inside. So here I check for the button selected and then I get inside the radioGroup that is in that layout and just set them false.

I have tried as well:

public void displayFalse(View view) {

    if(view.getId() == R.id.btnPrimers){

        rbPrimer1.setChecked(false);
        rbPrimer2.setChecked(false);
        rbPrimer3.setChecked(false);
        rbPrimer4.setChecked(false);

    }else if(view.getId() == R.id.btnSegons){

        rbSegon1.setChecked(false);
        rbSegon2.setChecked(false);
        rbSegon3.setChecked(false);
        rbSegon4.setChecked(false);

    }else if(view.getId() == R.id.btnSegons) {

        rbPostre1.setChecked(false);
        rbPostre2.setChecked(false);
        rbPostre3.setChecked(false);
        rbPostre4.setChecked(false);

    } else {

    }

But it doesn't either work. How can I fix the issue and is there a improved version?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Raül
  • 137
  • 3
  • 15

2 Answers2

2

RadioGroup has a method:

clearCheck()

It does exactly what you want:

public void displayFalse(View view) {

    if (view.getId() == R.id.btnPrimers)
        rgSegon.clearCheck(); //is this the right RadioGroup?
    } else if (view.getId() == R.id.btnSegons) {
        rgSegon.clearCheck();
    } else if (view.getId() == R.id.btnSegons) { //is this the right ID?
        rgPostre.clearCheck();
    }

}
TheWanderer
  • 16,775
  • 6
  • 49
  • 63
1

Try This:

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radiogroup);radioGroup.clearCheck();