Android Question ToggleButton - How to tell human press from programmatic change?

doncx

Active Member
Licensed User
Longtime User
I have a ToggleButton that may change it's default state (checked or not checked) based upon other values in the environment.

If the environment changes in certain ways, then the state of the ToggleButton should be changed UNLESS the user has set the ToggleButton state manually, in which case it's state should be respected and not changed.

I can't figure out how to trap the human press event on the ToggleButton, which is necessary to disable the re-defaulting.

How can I tell if a person has actually pressed the ToggleButton?

Thanks for your ideas.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code will help you:
B4X:
Sub Globals
   Dim IgnoreToggleEvent As Boolean
   Dim HasUserChangedValue As Boolean
   Dim tb As ToggleButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
   tb.Initialize("tb")
   Activity.AddView(tb, 0, 0, 100dip, 100dip)
   tb.Checked = True
End Sub

Sub tb_CheckedChange(Checked As Boolean)
   If IgnoreToggleEvent Then Return
   HasUserChangedValue = True
   Log("User changed")
End Sub

Sub Activity_Click
   IgnoreToggleEvent = True
   tb.Checked = Not(tb.Checked)
   IgnoreToggleEvent = False
End Sub
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I think you will need to set a global boolean each time you programmatically change the button and check the state of the boolean each time the event is fired, then reset it again ready for the next event.

Regards,
RandomCoder


Edit: Looks like Erel beat me to a reply.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I was a bit slow, this is similar principle to Erel's but will allow managing more than one toggle button:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim TB1Auto As Boolean
    Dim TB1Manual As Boolean
    Dim TB1ManualState As Boolean
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 TB1 As ToggleButton
    Private btn1 As Button
    Private Btn2 As Button
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("1")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub TB1_CheckedChange(Checked As Boolean)
    If Not(TB1Auto) Then
        TB1Manual = True
        TB1ManualState = Checked
    Else
        If TB1Manual AND Checked <> TB1ManualState Then
            TB1.Checked = TB1ManualState
        End If
        TB1Auto = False
    End If
End Sub
Sub btn1_Click
    'Simulate an auto Update
    TB1Auto = True
    TB1.Checked = Not(TB1.Checked)
End Sub
Sub Btn2_Click
    'Clear the manual update field
    TB1Manual = False
End Sub

And you'll need to set the AutoTB global whenever the program updates the state.

I've used a similar process to manage sliders in the past, without the manual lock.
 

Attachments

  • TBManager.zip
    7.1 KB · Views: 247
Last edited:
Upvote 0

doncx

Active Member
Licensed User
Longtime User
Thanks, Erel. A variation of this will do the trick nicely. Stevel05's post is also helpful. Thanks to you both.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…