2

Pretty simple question: I have a button which changes appearance on ACTION_DOWN and ACTION_UP. How do I avoid ACTION_UP being triggered if they have pressed down, kept their finger on the screen and moved their finger away from the button? Currently if they press down and slide their finger away from the button, it is still DOWN. Then when they release their finger from the screen, the button is still triggered...this is not the behaviour I want.

For example if you see tinder's main like/dislike buttons - once the finger is moved away from the button (if held down on the screen) the button action is cancelled (once the finger is released).

Thanks! It is a simple but specific question which I can't seem to find the answer to anywhere. It must be possible if tinder does it I guess

pauloz1890
  • 780
  • 1
  • 10
  • 22
  • I think it is definitely possible. Although I would think if you really want this functionality. Imagine that the user hits the button down, drags his finger off the button, the image will stay the same for that button. Now if the user wants to hit the button again should the button show the button_down image of the button_up? – David.Jones Jan 11 '15 at 02:35
  • User presses button and drags finger away from button -> as soon as the finger leaves the button area the button should go back to OFF state (button up image). That's how tinder does it anyway and it works quite well – pauloz1890 Jan 11 '15 at 02:38
  • Ok, Temporary solution, You can implement `onTouchListener()` and use `ACTION_MOVE` to identified area of View. – user370305 Jan 11 '15 at 02:46
  • 1
    Have you tried using ACTION_CANCEL? – David.Jones Jan 11 '15 at 02:55
  • Something similar http://stackoverflow.com/questions/12980156/detect-touch-event-on-a-view-when-dragged-over-from-other-view – user370305 Jan 11 '15 at 02:56

1 Answers1

0

If possible, you want to set the button background (or source image for ImageButton) to a Selector drawable. If you do this then all the state changes will be handled for you.

To answer the question, Views will continue to receive touch events until the View#onTouchEvent() returns false or until an ACTION_UP event is called which indicates the last finger has stopped touching the screen. So, if you don't want ACTION_UP to be called, then you simply return false when the event occurs that you don't want.

Community
  • 1
  • 1
DeeV
  • 35,865
  • 9
  • 108
  • 95
  • Unfortunately on ACTION_DOWN and ACTION_UP I'm starting an animation to change the appearance. SO I'm not using a selector. – pauloz1890 Jan 11 '15 at 03:08
  • The other parts will work. Detect when the finger is out of range in ACTION_MOVE events and return false when it is. – DeeV Jan 11 '15 at 03:13