0

I have a button and a text label displaying a number. I want to click the button to add the number of the label to the button. I want to also enable holding on the button for a while and later start to add the number continuously. If the longclick event has added the number, the click event will do nothing. How can I implement this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
tinaJohnny
  • 163
  • 2
  • 9

1 Answers1

0

Use a custom TouchListener like this (this is very basic, written in the browser, not in an IDE):

boolean touching = false;
long startTime = 0;

@Override
public boolean onTouchEvent(MotionEvent e) {

switch (e.getAction()) {
    case MotionEvent.ACTION_DOWN:
    if(touching){
        onLongClick(System.currentTimeMillis() - startTime)
    }else{
        touching = true;
        startTime = System.currentTimeMillis;
        onClick();
    }
    break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
       touching = false;
    break;
   }
return true;
}

onLongClick(long elapsedTime){
//Do stuff
}
David Corsalini
  • 7,958
  • 8
  • 41
  • 66
  • I think you could use e.getDownTime() instead of startTime, but I'm just reading it on the doc, never actually used. – David Corsalini Sep 09 '14 at 15:01
  • which function is this onLongClick? The parameter you passed is time interval. Any way, I think you code under if(touching) else{..} will never be called cause a action_up must be followed by a action_down event. – tinaJohnny Sep 11 '14 at 11:49
  • ACTION_DOWN will be called several times if you hold down your finger. ACTION_UP/CANCEL will be called when you lift your finger. onLongClick(long) is a method that you implement, you will have an elapsed time to know for how long the button was pressed. – David Corsalini Sep 11 '14 at 13:14