41

I have a radio group which I do not want to user to be able to select any of the buttons until a particular checkbox is selected within my app. If the checkbox is unticked then this disables the radio-group. How do I go about doing this.

W0lf7
  • 729
  • 1
  • 8
  • 15

8 Answers8

80

The real trick is to loop through all children view (in this case: CheckBox) and call it's setEnabled(boolean)

Something like this should do the trick:

//initialize the controls
final RadioGroup rg1 = (RadioGroup)findViewById(R.id.radioGroup1);
CheckBox ck1 = (CheckBox)findViewById(R.id.checkBox1);

//set setOnCheckedChangeListener()
ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton checkBox, boolean checked) {
        //basically, since we will set enabled state to whatever state the checkbox is
        //therefore, we will only have to setEnabled(checked)
        for(int i = 0; i < rg1.getChildCount(); i++){
            ((RadioButton)rg1.getChildAt(i)).setEnabled(checked);
        }
    }
});

//set default to false
for(int i = 0; i < rg1.getChildCount(); i++){
    ((RadioButton)rg1.getChildAt(i)).setEnabled(false);
}
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
  • It is nice to know that the "dirty hack" I did seems to be the actual way of doing it. :D – WORMSS Jul 11 '13 at 08:00
  • 1
    Setting enabled as false hides the radio button's "radio". It results in a very bad UX. I agree with the setClickable(false) in Kishita's answer. – Ahmed Shahid Apr 09 '22 at 11:46
  • 1
    You can change `setEnabled` within the loop to `setClickable` as you see fit. The algorithm will still work either way. – ariefbayu Apr 10 '22 at 23:36
11

If you have just a few radio buttons, better way would be to setClickable(false) for all children

radiobutton1.setClickable(false);
radiobutton2.setClickable(false);
radiobutton3.setClickable(false);
Kishita Variya
  • 810
  • 9
  • 19
8

RadioGroup cannot be disabled directly, we have to loop through the radio button and setEnabled as false.

// To disable the Radio Buttons of radio group.
    for (int i = 0; i < radioGroup.getChildCount(); i++) {
        radioUser.getChildAt(i).setEnabled(false);
    }
2

If you want to implement the same for Kotlin, Here's my code-> where rgNotificationType is the radio group name.

for (i in 0 until rgNotificationType.childCount) {
        (rgNotificationType.getChildAt(i)).isEnabled = false
    }
Manisha Saini
  • 51
  • 1
  • 7
1

By using kolin's extensions:

fun RadioGroup.setChildrenEnable(enable: Boolean) {
    for (i in 0 until this.childCount) {
        this.getChildAt(i).isEnabled = enable
    }
}

And in your code you can just call the function like this:

radioGroup.setChildrenEnable(false)
bebosh
  • 806
  • 10
  • 25
0

You can use the onCheckedChangeListener on your CheckBox and use the method setEnabled on your RadioGroup.

Best wishes, Tim

Tim
  • 6,692
  • 2
  • 25
  • 30
-1

Kotlin Solution

for (index in 0..radio.childCount - 1)
    radio.getChildAt(index).isEnabled = false
Roman
  • 2,464
  • 2
  • 17
  • 21
-3

Take actions according to the state of the checkbox and set the radiogroup accordingly. Assuming that you have a radio-group named radiogroup you can enable or disable the radiogroup by

radiogroup.setEnabled(true);

Add a OnCheckedChangeListener() to your checkbox.

Orlymee
  • 2,349
  • 1
  • 22
  • 24