Android Question button press and hold for a specified time then need to do some task

sunil thomas

Member
Licensed User
Longtime User
I need to press button and hold for a specified time ( for 4-5 seconds)then need to do some task, I didn't see any option for it.
Can you help..
 

DonManfred

Expert
Licensed User
Longtime User
you just can decide between a normal click and a long click.

B4X:
Sub Button1_Click
End Sub
Sub Button1_LongClick
End Sub
 
  • Like
Reactions: eps
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You could try using the Button_Down and Button _Up callbacks with a timer.

B4X:
Sub Button1_Down
   
End Sub
Sub Button1_Up
   
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Something like this:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim ButtonTimer 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.
    Dim 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")
    ButtonTimer.Initialize("B1Timer",4000)
End Sub

Sub Activity_Resume
  
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    ButtonTimer.Enabled = False
End Sub
Sub Button1_Down
    ButtonTimer.Enabled = True
End Sub
Sub Button1_Up
    ButtonTimer.Enabled = False
End Sub
Sub B1Timer_Tick
    Log("B1timer_Tick")
    'We got here so the button must still be pressed
    ButtonTimer.Enabled = False
    'Do Whatever
End Sub

Tags: Button Press Time

@DonManfred They do appear in the generate members list.
 
  • Like
Reactions: eps
Upvote 0

sunil thomas

Member
Licensed User
Longtime User
Button
A Button view. If you change the button’s background, you will usually want to use
StateListDrawable which allows you to set the "default" Drawable and the "pressed"
drawable.
This is an Activity object; it cannot be declared under Sub Process_Globals.
Events:
Down
Occurs when the user first presses on the button.
Up
Occurs when the user releases the button.
Click
Occurs when the user presses and releases the button. The Down and Up events also fire.
They are called in this sequence: Down, Up, Click.
LongClick
Occurs when the user presses on the button for roughly one second. The Down and Up events
also fire. They are called in this sequence: Down, LongClick, Up.

these are the events associated with button. Before click event get fired, button down and up is being fired.
 
Upvote 0
Top