1

I want that when you are holding a button during 3 secs do something, I try with the code above but it only do semething when I stop holding not during the hold. Should I implement a listener or something like this?

finishPushing = true;
.
.
.    

button.setOnTouchListener(new OnTouchListener()
            {

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

                    if(android.os.SystemClock.elapsedRealtime() - event.getDownTime() > 3000 && (event.getPointerCount() == 1) && finishPushing)
                    {
                        // Do something                     

                        finishPushing = false;
                    }
                    else{
                        finishPushing = true;
                    }
                    return false;
                }

            });
ƒernando Valle
  • 3,634
  • 6
  • 36
  • 58
  • 1
    You're on the right way, but you forget that this method just gets fired on new touch events. While you're holding it down, the last event is just the key_down, and the next will be the key_up. You could just start a little loop (make sure its async) which will count to 3 before doing something. Cancel it if you receive a key_up before the 3 sec pass by. – Stefan de Bruijn Mar 12 '13 at 09:21
  • yep I figured it, so I have to implementing a listener isnt it? – ƒernando Valle Mar 12 '13 at 09:22
  • 1
    That's one way. You could also start a new thread (AsyncTask would be easy) and launch your action from there if it passes. You can call .cancel() on AsyncTask. Just keep in mind that that doesn't actually cancel the task, it just sets a flag and you have to check inside your AsyncTask for isCancelled(). – Stefan de Bruijn Mar 12 '13 at 09:24
  • I found this post I did something similar and it works. http://stackoverflow.com/questions/5278579/android-touch-event-determining-duration – ƒernando Valle Mar 12 '13 at 09:34
  • Great , be sure to upvote his answer ;) – Stefan de Bruijn Mar 12 '13 at 09:36

1 Answers1

3

SOLVED thanks to Android Touch Event determining duration

        button.setOnTouchListener(new OnTouchListener()
        {
            private final Handler handler = new Handler();
            private final Runnable runnable = new Runnable() {
                public void run() {
                     if(mBooleanIsPressed)
                     {
                         // do whatever
                     }
                }
            };


            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN) {
                    // Execute your Runnable after 5000 milliseconds = 5 seconds.
//After this 5secs it will check if is pressed
                    handler.postDelayed(runnable, 5000);    
                    mBooleanIsPressed = true;               
                }

                if(event.getAction() == MotionEvent.ACTION_UP) {
                    if(mBooleanIsPressed) {
                        mBooleanIsPressed = false;
                        handler.removeCallbacks(runnable);
                    }
                }
                return false;
            }
        });
Community
  • 1
  • 1
ƒernando Valle
  • 3,634
  • 6
  • 36
  • 58