0

I'm stuck with a (maybe) little problem, I searched at Google and SO but nothing found..

What I would like is to fire a regular timed button click event (each seconds for example) when the user keeps his finger on the button (not a long click event).


Basically, I have a button and a counter variable.

cpt <- 0
User --CLICK--> Button
cpt <- 1
...  // 1 second later (Button still clicked)
cpt <- 2
...  // 1 second later (Button still clicked)
cpt <- 3
...

Thanks!

Val
  • 762
  • 8
  • 32
  • 1
    Look into MotionEvent or TouchEvent. You can start a time onDown (when the finger touches the screen) and stop it when lifted. – Rarw Mar 26 '14 at 14:00
  • Looks like what I'm trying to do ! Please share a snippet in an answer and i'll accept it (if it works, of course ^^) – Val Mar 26 '14 at 14:09

2 Answers2

1

Use an onTouchListener on your button to capture when it is touched and then handle the motion even accordingly. Below is an example but you can read more about handling touch events at the link provided

Button button = (Button) findViewById(R.id.my_button);
button.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //start timer here
                break;
            case MotionEvent.ACTION_UP:
                //stop timer
                break;
         }

     }
});

Starting the timer is more complicated. There are a number of ways to do this. This post has some good ideas. If you have a limited number of actions you need to trigger, I would consider using a Handler and creating a Runnable for each series of events. If the first action you want to happen occurs after 5 seconds, use the postDelayed method for Handler to call the appropriate Runnable after that amount of time. By chaining calls to handler.postDelayed() you can start different actions the longer the button is held. To cancel the action on ACTION_UP simply removeCallbacks on your handler for that Runnable. The sample code is in the link I attached.

The other methods, which are interesting but that I have never used, involve looking at the time the touch occurred and then subtracting the start time from system time to get the length of the action. Interesting concept, however, you would need to modify the code slightly to make it work since, in my experience, holding a finger on the screen continuously generates touch events which would make calculating duration useless.

private MotionEvent touch == null;

@Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction == MotionEvent.ACTION_DOWN){
            if(touch == null)
                 touch = event;

            long duration = android.os.SystemClock.elapsedRealtime() 
            - touch.getDownTime();

            if(duration == THRESHOLD){
               //do something 
            }
       }
     }

I have not tested that code but you can see the idea. By checking the duration of the initial down touch you could fire off different actions over time.

Community
  • 1
  • 1
Rarw
  • 7,645
  • 3
  • 28
  • 46
0

A similar question like this has been asked before, here take a look at this stack overflow question

To create the effect of starting and stopping the timer you would most likely need some kind of boolean or while loop.

Community
  • 1
  • 1
gollum18
  • 182
  • 1
  • 8
  • In the selected answer, `setOnLongClickListener`, I do not want a `LongClick` that should not resolve my problem.. – Val Mar 26 '14 at 13:56