Android Question Simulating togglebutton group

dexMilano

Active Member
Licensed User
Longtime User
Hello All,
I want to use a group o 3 togglebuttons to use in group
(if one is selected the others are automatically unselected).
I tried to do by myself but I'm having some issue.
Particularly because the change of the toggle is happening before the execution of my code (I suppose)

I've found other post with related topic but no solution/code.

Below my solution.
Any comment/clue is useful.

I've 3 buttons in the Designer
and I'm managing in group sharing the event and checking the sender.
I'm using tags to manage the status
Also there is a variable (selected_metric) to keep current selected (maybe this could be not necessary)

This code seems to work but sometime fails above all when tapping on already active button
:-(

TIA
in the meantime I'm continuing to work on it

dex


B4X:
Sub btn_g1_CheckedChange(Checked As Boolean)
Dim s As String
Dim b As ToggleButton

    b = Sender
    s = b.Tag & ":" & b.Checked & selected_metric
    Log (s)
  
    If selected_metric = b.Tag Then
        b.Checked = True
    Else
        Select b.Tag
        Case "time"
            btn_time.Checked = True
            btn_bpm.Checked = False
            btn_hz.Checked = False
        Case "bpm"
            btn_time.Checked = False
            btn_bpm.Checked = True
            btn_hz.Checked = False
        Case "hz"
            btn_time.Checked = False
            btn_bpm.Checked = False
            btn_hz.Checked = True
        End Select
    End If
  
    selected_metric = b.Tag
End Sub
 

dexMilano

Active Member
Licensed User
Longtime User
Hello NJdude,
I tried your code (you can find my version below)

Unfortunately it doesn't work at all.
Logging event I see that every time I change the status by code a new event is arisen so the system goes confusing.

:-(

B4X:
Sub btn_g1_CheckedChange(Checked As Boolean)
Dim s As String
Dim b As ToggleButton

    b = Sender
    s = selected_metric & " > " & b.Tag
    Log (s)
   
    btn_time.Checked = False
    btn_bpm.Checked = False
    btn_hz.Checked = False
   
    Select b.Tag
    Case "time"
        btn_time.Checked = True
    Case "bpm"
        btn_bpm.Checked = True
    Case "hz"
        btn_hz.Checked = True
    End Select
   
    selected_metric = b.Tag
End Sub
 
Upvote 0

dexMilano

Active Member
Licensed User
Longtime User
Analysis above not correct

this is an example of the output log

bpm > bpm
bpm > time
bpm > hz
time > hz
time > time

bpm > hz
bpm > time
bpm > bpm
time > bpm
time > time

Every 1 tap generates 5 ticks.

dex
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim ToggleButton1 As ToggleButton
   Dim ToggleButton2 As ToggleButton
   Dim ToggleButton3 As ToggleButton
   Private group As List
   Private ignoreCheckedEvent As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   group.Initialize
   group.Add(ToggleButton1)
   group.Add(ToggleButton2)
   group.Add(ToggleButton3)
End Sub



Sub ToggleButton_CheckedChange(Checked As Boolean)
   If ignoreCheckedEvent OR Checked = False Then Return
   ignoreCheckedEvent = True
   Dim tb1 As ToggleButton = Sender
   For Each tb As ToggleButton In group
     If tb <> tb1 Then tb.Checked = False
   Next
   CallSubDelayed(Me, "ClearEventLock")
End Sub

Sub ClearEventLock
   ignoreCheckedEvent = False
End Sub
 
Upvote 0

dexMilano

Active Member
Licensed User
Longtime User
Hello Erel,
I tested your code but it doesn't work when you tap in an already selected button.
it switches leaving OFF, so any button is OFF.
Instead the logic should be if ON not to switch at all.

Analyzing the code it is very clear the scenario.
Could you tell me something kore about the
CallSubDelayed()


I never used before.

And I didn't find any reference in the manuals (beginner's or user's)

TIA
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Hello Erel,
I tested your code but it doesn't work when you tap in an already selected button.
it switches leaving OFF, so any button is OFF.
Instead the logic should be if ON not to switch at all.

Analyzing the code it is very clear the scenario.
Could you tell me something kore about the
CallSubDelayed()


I never used before.

And I didn't find any reference in the manuals (beginner's or user's)

TIA

Using CallSubDelayed to interact between activities and services
 
Upvote 0

dexMilano

Active Member
Licensed User
Longtime User
Hello all,
A possible workaround could be to disable the ON button (enable = false).
(I treid and it worked)
But graphically I really don't like it.

I think that the point is that the graphic switch is executed before the click code.
Do you think that it could be possible to intercept the event before?

dex
 
Upvote 0
Top