0

I got a radio button in a radio group. It is dynamically created, so I haven't got the reference to the radio group the button is in. I would like to make it able for the app user to uncheck a radiobutton by again clicking on it. I tried a view things but couldn't succeed.

How do I do this?

I tried the following code:

rButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (((RadioButton) v).isChecked()) {
            ((RadioButton) v).setSelected(false);
        }
    }
});

Problem is, that this if-clause is always true also if you press the radiobutton for the first time. AND more important the setSelected(false) doesn't work.

Thank you for any advice.

Edit: I mad this foolish mistake of using setSelected instead of setChecked. So with setChecked I can uncheck the radioButton. But so it is never checked, not even the first time.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
progNewbie
  • 4,362
  • 9
  • 48
  • 107
  • you would probably have to create a button to unselect it. – letsCode Feb 19 '18 at 20:34
  • try in OnClick rButton.setChecked(false); – TREAF ALSHEMERI Feb 19 '18 at 20:48
  • @ElMaravilla please see my edit – progNewbie Feb 19 '18 at 21:06
  • if you cheking isChecked you must setChecked, if you checking isSelected you must setSelected – Marcos Vasconcelos Feb 19 '18 at 21:08
  • If it appears an error when you click on the radio button then try this : if(rButton.isChecked()){ rButton.setChecked(false); } – TREAF ALSHEMERI Feb 19 '18 at 21:10
  • @MarcosVasconcelos like I wrote in my Edit, I already do this now. – progNewbie Feb 19 '18 at 21:11
  • If it appears an error when you click on the radio button then try this : if(rButton.isChecked()){ rButton.setChecked(false); } – TREAF ALSHEMERI Feb 19 '18 at 21:11
  • @ElMaravilla There is no error. The problem is, that I can't check a radiobutton in the app now. Because everytime I check it it gets directly unchecked. – progNewbie Feb 19 '18 at 21:11
  • try my answer now please. – TREAF ALSHEMERI Feb 19 '18 at 21:14
  • Don't use RadioButtons if you want CheckBoxes. – Phantômaxx Feb 19 '18 at 22:31
  • @KlingKlang I don't want Checkboxes, that's why I am using radio buttons ;-) – progNewbie Feb 20 '18 at 00:27
  • But you want them to behave like CheckBoxes. Only CheckBoxes have the ability to be deselected. That's why I posted my comment. – Phantômaxx Feb 20 '18 at 07:58
  • @KlingKlang No I don't. In general I want them to behave like radiobuttons: I want that all other radiobuttons get unchecked when one of the radiobuttons in the same radiogroup are checked. (Typical radiobutton behaviour) I also want them to look like radiobuttons. I just want the possibility to also uncheck them. (yes this is typically checkbox behaviour). But so it is a symbiosis of radiobuttons and checkboxes. I don't want to use checkboxes and let them look like radio buttons..that would be stupid. – progNewbie Feb 20 '18 at 10:55
  • The point is... why do you want RadioButtons to behave **unnaturally**? People expects RadioButtons to follow their standard, rigid behavior: "one of many". "None" is not contemplated. – Phantômaxx Feb 20 '18 at 11:48
  • @KlingKlang Why does it matter WHY I want this? – progNewbie Feb 20 '18 at 11:55
  • Because it doesn't make sense to me. – Phantômaxx Feb 20 '18 at 11:59
  • It doesn't need to make sense to you. It is just that I want the app-user to choose between different options. But he should only be able to check one option (that's why radio button). But at the same time it shouldn't be mandatory. – progNewbie Feb 20 '18 at 12:16
  • RadioButtons **are** mandatory. CheckBoxes **aren't**. You choose. OR you can write your own custom View. – Phantômaxx Feb 20 '18 at 15:27

0 Answers0