Android Question Detect how many seconds a button is pressed?

Discussion in 'Android Questions' started by Mashiane, Jul 14, 2015.

  1. Mashiane

    Mashiane Well-Known Member Licensed User

    Hi

    I need some help please and perhaps an example of detecting how long a button has been pressed and also how to fire an event when a particular time frame has elapsed whilst the button is pressed?
     
  2. NJDude

    NJDude Expert Licensed User

    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.
     
    thedesolatesoul and Mashiane like this.
  3. Mashiane

    Mashiane Well-Known Member Licensed 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?

     
  4. DonManfred

    DonManfred Expert Licensed User

    Code:
    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"
     
    Mashiane and eurojam like this.
  5. eurojam

    eurojam Well-Known Member Licensed 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....

    Code:
    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
     
    Mashiane, MikeH and DonManfred like this.
  6. Mashiane

    Mashiane Well-Known Member Licensed 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.
     
  7. DonManfred

    DonManfred Expert Licensed User

    use a timer. Start the timer on DOWN and stop it on UP
    Use the timer tick to update the label...

    Just ignore the timer tick events
     
Loading...