My Radio Buttons not working correctly

wdegler

Active Member
Licensed User
Longtime User
The following does not work (not even in a Panel). What am I missing?

Sub Globals
Dim Rad1,Rad2 As RadioButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
Rad1.Initialize("Rad1")
Rad1.Checked=True
Rad1.Text="RB1"
Activity.AddView(Rad1,1%x,1%y,50%x,35%y)

Rad2.Initialize("Rad2")
Rad2.Text="RB2"
Activity.AddView(Rad2,1%x,15%y,50%x,35%y)
End Sub

Sub Rad1_CheckedChange(Chkd As Boolean)
If Chkd Then
Msgbox("RB1 Pressed","")
End If
End Sub

Sub Rad2_CheckedChange(Chkd As Boolean)
If Chkd Then
Msgbox("RB2 Pressed","")
End If
End Sub
 

wdegler

Active Member
Licensed User
Longtime User
What this code is doing for me ...

I copied the code from this message as is into a new app and compiled it. Result:

When I start the program, the two radio buttons appear with the first one highlighted as expected. When I touch RB1, then RB2 highlights and the RB2 message box appears. After I close it, no further touches elicit a response.

When I start the program again and touch RB2, the RB2 message box appears again. After I close it, again, no further touches elicit a response.

What is this code doing for you?
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
It works as you would expect it to, toggling the radio buttons and showing the appropriate msgbox. It is a bit awkward as you have the button sizes set so that they overlap, maybe that is your problem.
 
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
Problem seems to be solved

Thanks for your attention to this matter. I believe the problem is now solved, not by a change in code but by a change in thinking.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
I encourage you to post the details, as your explanation of the solution may help others with the same issue.
 
Upvote 0

MDEnt

Member
Licensed User
Longtime User
After playing with this example to implement radio buttons in designer - I noticed that the state of the radio button isn't saved. I'm likely doing something very simple wrong? I have this code with 3 buttons to match in designer. I have a separate activity module that is called and shows a view with the 3 radio buttons. The message boxes confirm the specific radio button was selected. If I leave this view and return, the last selected radio button has been cleared.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
When you leave an Activity and return you will get the state you defined in the Designer because you load the layout file again.
You must define a Process_Global variable memorizing what RadioButton is selected and set it to checked in the Activity_Resume routine.

Best regards.
 
Upvote 0

MDEnt

Member
Licensed User
Longtime User
ok - question time again!

I have declared the process global in my main module textFontC As Int.
My activity module called settings has the view with the 3 radio button setup in designer with whiteRadio set to checked true.

in the activity resume in the setting module I have:

Sub Activity_Resume
Msgbox(Main.textFontC,"")
If Main.textFontC = 1 Then
whiteRadio.Checked = True
Else If Main.textFontC = 2 Then
blackRadio.Checked = True
Else If Main.textFontC = 3 Then
grayRadio.Checked = True
End If
End Sub

When I return to the settings module - I get the message box stating the correct value - but no change in the button that should be checked.

Would the settings activity_create itself (where the button selection occurs) be aware of the true value from the resume activity - or would I have to force feed it the true value somehow?

Thanks!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Could you post your project as a zip file ?
It would be much easier to help you, otherwise we must try to guess what you have done besides the code snippet you posted or event create a small project for testing.
Do you update textFontC in the CheckedChange routines?
The code in your last post should work.

Best regards.
 
Upvote 0

MDEnt

Member
Licensed User
Longtime User
Unfortunately I cant post the project here - I may try create a small project to test.

But to answer your question, yes the textFontC is updated when a radio button is selected - such as...

Sub whiteRadio_CheckedChange(Chkd As Boolean)
If Chkd Then
Main.textFontC=1
End If
End Sub

Sub blackRadio_CheckedChange(Chkd As Boolean)
If Chkd Then
Main.textFontC=2
End If
End Sub

I've placed message boxes everywhere that are showing me the correct textFontC value. And of course the font color is changing in the main module activity, which is the point of the radio buttons. It just seems that resume is being stubborn in setting which radio button appears selected when revisiting the settings activity module.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Unfortunately I cant post the project here - I may try create a small project to test.
Its always the best to post either the whole project or a small project showing the problem.

Best regards.
 
Last edited:
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Where in your code are you loading the layout, as it sounds like the layout is being loaded AFTER Activity_Resume has run (worth checking for all ocurrences of LoadLayout).
 
Upvote 0

MDEnt

Member
Licensed User
Longtime User
Here is a settings activity module. the designer has the whiteRadio as checked by default. The module does send the main activity the correct value to change the font color (font color changes fine) - the only problem I'm having is figuring out where to confirm that the correct radio button appears checked when returning to this activity to change the font color again. It always shows as whiteRadio even if one of the other two are previously chosen.

Enjoying using b4A, appreciate the help, and still learning...

'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub

Sub Globals
Dim whiteRadio, blackRadio, grayRadio As RadioButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("settingscreen") 'Load the layout file.
whiteRadio.Initialize("whiteRadio")
whiteRadio.Text="White"
blackRadio.Initialize("blackRadio")
blackRadio.Text="Black"
grayRadio.Initialize("grayRadio")
grayRadio.Text="Gray"
End Sub

Sub WhiteRadio_CheckedChange(Chkd As Boolean)
If Chkd Then
Main.textFontColor=1
End If
End Sub

Sub blackRadio_CheckedChange(Chkd As Boolean)
If Chkd Then
Main.textFontColor=2
End If
End Sub

Sub grayRadio_CheckedChange(Chkd As Boolean)
If Chkd Then
Main.textFontColor=3
End If
End Sub

Sub Activity_Resume
If Main.textFontColor = 1 Then
whiteRadio.Checked = True
Else If Main.textFontColor = 2 Then
blackRadio.Checked = True
Else If Main.textFontColor = 3 Then
grayRadio.Checked = True
End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Last edited:
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Your problem is here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("settingscreen") 'Load the layout file.
whiteRadio.Initialize("whiteRadio")
whiteRadio.Text="White"
blackRadio.Initialize("blackRadio")
blackRadio.Text="Black"
grayRadio.Initialize("grayRadio")
grayRadio.Text="Gray"
End Sub
You must not initialize views if they are loaded as part of a layout file.

Do a forum search for a detailed reason.
 
Upvote 0
Top