Android Question Detect how many seconds a button is pressed?

Mashiane

Expert
Licensed User
Longtime User
Thanks, will try and write something. Kinda lazy though. Can you write something for me that will fire an event after a 3 seconds press that is bug-less? How much can I donate for that?

You could use a timer, start it on the DOWN event and stop it on the UP event, also on the same event calculate the elapsed time.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
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
    Private Label1 As Label
    Private pressed As Long
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")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Button1_Down
    pressed = DateTime.Now  
End Sub
Sub Button1_Up
    Dim pressedTime As Long = DateTime.Now - pressed
    Label1.Text = pressedTime & "ms"
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
Following the idea from NJDude you can define a touch event for the button with the reflection lib. then you take the time between the DOWN and UP event like NJD described, in this case I only take the actual time and take the difference in ticks....

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 b As Button
    Dim r As Reflector
    Dim t1, t2 As Long
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")
    b.Initialize("B")
    Activity.AddView(b,0,0,100dip,100dip)
    r.Target = b
    r.SetOnTouchListener("B_OnTouch")
End Sub

Sub B_OnTouch( viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
  Select action
    Case Activity.ACTION_DOWN
      t1 = DateTime.Now

    Case Activity.ACTION_UP
      t2 = DateTime.Now
      Log(t2-t1)
    End Select
    b.Invalidate   
    Return True
End Sub
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Thanks guys, I get you , this looks very interesting. Just two more last things.

1. How do you then whilst the button is pressed down, on another label control show the elapsed time in seconds? The label will first show 1, then 2 then 3 whilst the button is still pressed.
2. How do you then make the program stop executing when for example a 5 second count is reached, even if the user is still holding the button.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1. How do you then whilst the button is pressed down, on another label control show the elapsed time in seconds? The label will first show 1, then 2 then 3 whilst the button is still pressed.
use a timer. Start the timer on DOWN and stop it on UP
Use the timer tick to update the label...

2. How do you then make the program stop executing when for example a 5 second count is reached, even if the user is still holding the button.
Just ignore the timer tick events
 
Upvote 0
Top