B4J Code Snippet Single RadioButton added in Designer does not toggle as it did when added in Scene Builder

This started off as a question and I felt like a newby as it was doing my head in, but I solved the problem and thought I'd share it.

I have used single RadioButtons in my projects since I began using B4J many years ago when using Scene Builder was the norm. The single RadioButton would toggle on/off on being clicked and I would use the RadioButton.Selected property to control the application according to my need. These applications continue to work as expected.

However recently I wanted to update an application for which I'd created the layout in Designer, by adding a single RadioButton. To my astonishment I found the RadioButton would not toggle - once selected, it could not be deselected.

Why the different behavior and how to achieve the desired toggling behaviour of a single RadioButton added to a Designer generated layout?

As per this reference "A RadioButton that is not in a ToggleGroup can be selected and unselected" - the functionality I desire. I assume Designer automatically assigns a RadioButton to a ToggleGroup, whereas by default Scene Builder does not and it has to be assigned to a ToggleGroup manually where the functionality of a group of RadioButtons is desired. The solution therefore is to set ToggleGroup property to Null for the RadioButton added via Designer.

Using this post by @stevel05 as a reference I implemented this code:

B4X:
'Remove ToggleGroup assigned to this Radiobutton
Public Sub removeToggleGroup (RB As RadioButton)
    Dim RBJO As JavaObject = RB
    RBJO.RunMethod("setToggleGroup",Array(Null))
End Sub

and so for each single RadioButton in the layout I call:

B4X:
Private RadioButton1 As RadioButton

removeToggleGroup(RadioButton1)

Perhaps this is "well known", but I couldn't find anything while searching the Forum and I can't see how the ToggleGroup property of a RadioButton can be edited in Designer.

Incidentally, ChatGPT kept telling me that I should use a CheckBox rather than a RadioButton because that has my desired functionality, but for consistency of appearance with my previous applications, I'd rather use RadioButtons.
 

LucaMs

Expert
Licensed User
Longtime User
Incidentally, ChatGPT kept telling me that I should use a CheckBox rather than a RadioButton
That's what I (and everyone else) was about to say, too, before reading the last line of your post.

Great solution, thanks to @stevel05 (B4J GURU!); however, it's worth considering that it's not cross-platform, and it's often convenient to write this type of source code in case you want to create a mobile version of the same project in the future.
 

stevel05

Expert
Licensed User
Longtime User
A togglegroup is assigned for all radiobuttons in a common parent, this also has the effect of adding a single radiobutton to it's own toggle group so it can no longer be toggled, as one of the buttons in the group needs to be selected.

You can use this Designer script extension to remove the toggle group from multiple buttons in one go.

B4X:
'Parameters: 1 or more RadioButtons
'DesignerScript: {DSE_Class}.RemoveToggleGroup(RadioButton1,Radiobutton2)
Public Sub RemoveToggleGroup(DesignerArgs As DesignerArgs)
     
    For i = 0 To DesignerArgs.Arguments.Size - 1
        Dim RB As JavaObject = DesignerArgs.GetViewFromArgs(i)
        RB.RunMethod("setToggleGroup",Array(Null))
    Next
 
End Sub

Where {DSE_Class} is whichever class the code is in.
 
Last edited:

bdunkleysmith

Active Member
Licensed User
Longtime User
Thanks for your additional input @stevel05

I'm not familiar with Designer scripts and I've tried to implement your code, but I'm running into errors despite reading Designer Script Extensions. So can you please advise where I'm going wrong?

I added:

B4X:
'Parameters: 1 or more RadioButtons
'DesignerScript: {DSE_Class}.RemoveToggleGroup(RadioButton1,Radiobutton2)
Public Sub RemoveToggleGroup(DesignerArgs As DesignerArgs)
    For i = 0 To DesignerArgs.Arguments.Size - 1
        Dim RB As JavaObject = DesignerArgs.GetViewFromArgs(i)
        RB.RunMethod("setToggleGroup",Array(Null))
    Next
End Sub

into Main and commented out my removeToggleGroup sub and the call to it. I then added:

B4X:
Main.RemoveToggleGroup(RadioButtonAll)

into the Script - General panel within Designer.

However the application won't run as it complains about Layout1.bjl and if I run the script in Designer (F5), a popup displays an error message:

Error.png
 

stevel05

Expert
Licensed User
Longtime User
The Designer Script code needs to be in a class, Main is a code module so will not work in there. If you don't have anywhere appropriate, you can just create a new class
 

bdunkleysmith

Active Member
Licensed User
Longtime User
Ah, of course! Thanks @stevel05, all working well now.

I've learned a few new things in the process of getting to this point which is great - don't they it's about the journey and not the destination!
 
Top