Android Question Check if a button is pressed repeatedly

trueboss323

Active Member
Licensed User
Longtime User
Hey,
Is it possible to check if a button is being pressed repeatedly? I want to make it so that the button can't be pressed repeatedly.
 

stevel05

Expert
Licensed User
Longtime User
You need to define what you mean by repeatedly. Do you mean that the button can only be pressed once. Or it can only be pressed once until another condition is met or another button is pressed. Or do you mean it can only be pressed once within x seconds or something else altogether?
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
You need to define what you mean by repeatedly. Do you mean that the button can only be pressed once. Or it can only be pressed once until another condition is met or another button is pressed. Or do you mean it can only be pressed once within x seconds or something else altogether?

By repeatedly , I mean rapidly pressing it, like 5+ times a second.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Then you can start a timer when the button is first pressed with a tick time of your required duration. Then when the button is pressed, check if the timer is running and ignore the click until the timer has stopped.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim ClickTimer As Timer
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 Button1 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")  'Layout just contains Button1
    ClickTimer.Initialize("ClickTimer",1000)
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    ClickTimer.Enabled = False
End Sub

Private Sub Button1_Click
    If ClickTimer.Enabled Then
        Log("Click Ignored")
        Return
    End If
    ClickTimer.Enabled = True
    Log("Process click")
   
End Sub

Private Sub ClickTimer_Tick
    ClickTimer.Enabled = False
End Sub
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Another option is to disable the Button for a brief period after a click:
B4X:
Sub Button1_Click
   DoSomeStuff
   Button1.Enabled = False
   Sleep(200) 'sleep for 1/5 of a second
   Button1.Enabled = True
End Sub
 
Upvote 0

Diceman

Active Member
Licensed User
Another option is to disable the Button for a brief period after a click:
B4X:
Sub Button1_Click
   DoSomeStuff
   Button1.Enabled = False
   Sleep(200) 'sleep for 1/5 of a second
   Button1.Enabled = True
End Sub


This should work too. I've added a ButtonDelayMS that determines how long the button should be disabled so it prevents double clicking.

B4X:
Sub Button1_Click
    Private ButtonDelayMS  As Long=1000         'Minimum delay for button in ms prevents button from being pressed twice within 1 second
    Private StartTime      As Long=DateTime.Now
    Button1.Enabled = False                     'Disable the button immediately before we do anything
    Try                                         'We must use Try/Catch block to make sure button is re-enabled before we leave
        'DoSomeStuff                            'Do what we gotta do
        Label1.Tag  = Label1.Tag + 1            'Do some action so we know button is not double pressed
        Label1.Text = Label1.Tag
        Sleep(Rnd(100,400))                     'Dummy delay so we use up some time for this example (remove this line in your code)
    Catch
        Log(LastException.Message)              'Catch the exception so it doesn't prevent re-enabling of button
    End Try
    Private RemainMS As Long=ButtonDelayMS-(DateTime.Now-StartTime)
    If RemainMS > 0 Then                       'Do we have remaining time to use up? (Button must use up at least ButtonDelayMS)
        Log("Sleeping for " & RemainMS & " ms")
        Sleep(RemainMS)
    End If
    Button1.Enabled = True                     'Make sure button is re-enabled when we exit
End Sub
 
Upvote 0
Top