Android Question RadioButton_CheckedChanged event not firing

LeeM

Member
Licensed User
Longtime User
I've added two radio buttons to a panel in the designer.
When a radio is checked I save a small text file and then reload this when the app is restarted so I can set the last used radio button.

I can save and reload the file OK and I set the checked property to True but this doesn't fire the CheckedChange event as I expected. I have to add the RadioButton1_CheckedChange(True) line. Not sure if this is the way it's supposed to behave or am I doing something wrong ?

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Dim SaveFileName As String = "SaveFile.txt"
    Dim SavedData As String
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Panel1 As Panel
    Private RadioButton1 As RadioButton
    Private RadioButton2 As RadioButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")

    If File.Exists (File.DirDefaultExternal, SaveFileName) = True Then
            SavedData = File.ReadString(File.DirDefaultExternal, SaveFileName)
            If SavedData = "1.txt" Then
                RadioButton1.Checked = True
                'It only works if I add the line below
                RadioButton1_CheckedChange(True)
            Else
                RadioButton2.Checked = True
                'It only works if I add the line below
                RadioButton2_CheckedChange(True)
            End If
    Else
            SavedData = "1.txt"
            RadioButton1.Checked = True
           
    End If
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub RadioButton1_CheckedChange(Checked As Boolean)
    File.WriteString(File.DirDefaultExternal, SaveFileName, "1.txt")
    'Do other stuff...
End Sub

Sub RadioButton2_CheckedChange(Checked As Boolean)
    File.WriteString(File.DirDefaultExternal, SaveFileName, "2.txt")
    'Do other stuff...
End Sub
 

mangojack

Expert
Licensed User
Longtime User
try ..
B4X:
Sub RadioButton1_CheckedChange(Checked As Boolean)
   If Checked Then
      File.WriteString(File.DirDefaultExternal, SaveFileName, "1.txt")
      'Do other stuff...
   End If
End Sub

Sub RadioButton2_CheckedChange(Checked As Boolean)
   If Checked Then
      File.WriteString(File.DirDefaultExternal, SaveFileName, "2.txt")
      'Do other stuff...
   End If
End Sub

I don't see the point writing the .txt extension as well ..

Edit .. You shouldn't need to call the line .. RadioButton x_CheckedChange(True)
 
Last edited:
Upvote 0

LeeM

Member
Licensed User
Longtime User

'If Checked Then' doesn't seem to work.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The event is raised. Test it with this code and you will see:

B4X:
Sub RadioButton1_CheckedChange(Checked As Boolean)
   If Checked Then
     Log("RadioButton1_CheckedChange")
   End If
End Sub

Sub RadioButton2_CheckedChange(Checked As Boolean)
   If Checked Then
     Log("RadioButton2_CheckedChange")
   End If
End Sub

You can use StateManager to store the UI state: https://www.b4x.com/android/forum/threads/9777/#content
 
Upvote 0

LeeM

Member
Licensed User
Longtime User
Hi Erel,

Thanks for replying.
The event is raised when I physically tap a radio button but not when I restart the app and it loads the saved file.
The .Checked = True line in Activity_Create does check the correct radio button but the event is not raised (no log data).

I'm using B4A version 5.00 if that makes any difference.
 
Upvote 0

JdV

Active Member
Licensed User
Longtime User
Hi

Is the _CheckedChange event name a little misleading in this case as it only seems to fire when the radio button is selected?

For example:
FirstRadioButton is selected. If another radio button in the group (SecondRadioButton) is selected, the _CheckedChanged event isn't fired for FirstRadioButton.

B4X:
Sub FirstRadioButton_CheckedChange(Checked As Boolean)
    If Checked=True Then
        Msgbox("Radio button is now checked", "")
    Else
        Msgbox("Radio button is now unchecked", "")
    End If
End Sub

Regards

Joe
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…