0

I have a radiogroup with about 30 radiobuttons. I have looked around on stackoverflow and found a few posts about the accidental allowing of multiple radiobuttons to be checked. They were not in a radiogroup, or had problems with their id.

https://stackoverflow.com/questions/8265034/android-radiogroup-checks-more-than-one-radiobuttonhttps://stackoverflow.com/questions/17157705/radiogroup-allows-multiple-radiobuttons-to-be-selected

How can I purposefully allow 3 radiobutton to be selected at a time and implement a listener for those three selected.

I suspect I am doing something wrong. Is there another UI element out there that can help me get what I am looking for?

Community
  • 1
  • 1
superuser
  • 731
  • 10
  • 28

1 Answers1

1

How can I purposefully allow 3 radiobutton to be selected at a time

Don't put them inside of a RadioGroup.

and implement a listener for those three selected.

You can use an onCheckedChangeListener

I suspect I am doing something wrong. Is there another UI element out there that can help me get what I am looking for?

You suspect correctly. As stated by Jason in a comment, you should be using CheckBoxes for this as this is not the behavior a user will expect from RadioButtons. Most users will expect to see a RadioGroup and only be able to choose a single option. If you have that many RadioButtons and want to be able to choose multiples then you should be able to split them up into separate groups. Otherwise, you definitely should be using CHeckBoxes as users will see those and expect to be able to choose multiples.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • thanks for the answer! Could you perhaps post some code about how to make only 3 checkboxes available to be checked? I just need the checkboxes to max out at 3. – superuser Oct 10 '13 at 23:39
  • @superuser: you were given exact same answer [here](http://stackoverflow.com/a/19307262/1893766), but have not accepted the answer there. What made you to accept this one? – ozbek Oct 10 '13 at 23:42
  • @shoerat I have accepted it, I was just waiting for some code to be posted. – superuser Oct 10 '13 at 23:45
  • @superuser I would just create a counter as a member variable and increment it when a `CheckBox` is checked and decrement it by 1 when a `CheckBox` is unchecked. When the counter reaches 3 you can disable the others and enable them when that number goes below 3. Instead of disabling them you could just do nothing when a fourth tries to be checked and print a `Toast` that they have reached the limit. But personally I would disable them (users will get the idea). Its not too complicated to at least give it a try. Do that and if you get stuck I will try to help – codeMagic Oct 10 '13 at 23:50
  • @codeMagic thanks, I will try your suggestion! – superuser Oct 10 '13 at 23:52
  • OK, good, I could give you code to do it but this is fairly basic so you really need to try on your own so you can learn. Then post if you get stuck. Good luck! – codeMagic Oct 10 '13 at 23:53