Home > Software design >  How to make a radio button fully toggleable?
How to make a radio button fully toggleable?

Time:08-20

I've been changing over some app menu options lately in order to simplify the graphics and give me more space to work with. I added a radio button, and have it set up to toggle on and off when pressed via a listener:

                View.OnClickListener toggleListener = view -> {
                Settings.getSetting().toggleSetting();
                holder.settingToggleButton.setChecked(!holder.settingToggleButton.isChecked());
                };

                ...
                holder.settingToggleButton.setOnClickListener(toggleListener);

But when I load into my app and toggle the button, it'll work initially (options that are on get toggled off, and vice versa) but when I then try to tap it again and toggle the option back on/off, it doesn't work. It sticks either on or off, whichever state it was initially toggled into when I first pressed it. I'm not sure why this is happening, as the logic is very straight-forward... any ideas as to why this set up isn't working?

CodePudding user response:

Have you tried giving the reference val with the id of that radio button?

Checkout RadioButtons at The Official Developer Android Website

CodePudding user response:

 holder.settingToggleButton.setChecked(!holder.settingToggleButton.isChecked());

The logic here is correct, as @Ansh suggested, I would say the problem is the UI never receives the setChecked attribute and what you're seeing is the default behavior of a radiobutton, which takes the initial input but isn't toggleable without logic code.

If you think that's not the case, try putting down a Button that updates the toggle state of a specific radiobutton. This'll help you see what you're doing wrong.

CodePudding user response:

I have discovered the answer to my question; it's a niche solution related directly to my problem, but overall, it was indeed related to not checking for/modifying the ID of the toggle button. If you're ever having issues with the button toggling one way but not the other, or not toggling at all, make sure all references to that button, and all the changes it's involved in, take it into account.

  • Related