I am working on application in which a user needs to press and hold a button for a long time.
How can I detect the moment that the user: finishes the press or moves their touch position?
Thanks
I am working on application in which a user needs to press and hold a button for a long time.
How can I detect the moment that the user: finishes the press or moves their touch position?
Thanks
I think you can use OnTouchListener for this.
These answers are pretty complicated.
onClick
from anOnClickListener
still gets called at the end of a long press if you returnfalse
. That's the easiest place to detect the end of a long press.This is especially important to know because if you implement the
onTouch
route while returningfalse
fromonLongClick
(the default AS gives you and often what you want), youronClick
code may be called at the end of your long presses without you realizing it.Here's an example based on capturing a photo or video:
As you can see, the logic becomes very straight forward when you let Android propagate the end touch event to an
OnClickListener
.I think the onFocusChanged-Listener can be used for this.
I think your best bet is to use a combination of the onLongClickListener() and onTouchListener() for that button. You'll need to catch certain events on the touch listener since it will trigger for every touch event.
Try something like the following: