I have a view that need to process onTouch gestures and onClick events. What is the proper way to achieve this?
I have an onTouchListener
and an onClickListener
set on the view. Whenever I do touch the view, first the onTouch
event is triggered and later the onClick
. However, from the onTouch
event handler I have to return either true
or false
. Returning true
means that the event is being consumed, so the android event system will not propagate the event any further.
Therefore, an onClick
event is never generated, atleast my onClick
listener is never triggered when I return true
in my onTouch
event handler. On the other hand, returning false
there is not an option, since this prevents the onTouch
listener from receiving any further events that are necessary in order to recognize a gesture. What's the usual way of solving this?
Another way is to use threads. That is on Action_Down start a thread to increment a counter. In case of Action_UP : stop/interrupt the thread. look for counter if it is less than 2 (say or threshold as per your application) or !isMove then invoke click function
I've been able to implement OnClick and OnTouch together in a custom keyboard I'm building. You can take a look at this code and modify it according to your context since you didn't include a code sample to work from.
If you post a sample of your code I can modify this sample of mine to accomplish the same outcome for your use. Context is everything when it comes to Android Development. Take a look at this code snippet and see if it helps. Otherwise post a sample of your own and I'll make the modifications and reply back.
This works for the context of type alpha or numerical output, if the event is looking to send or consume some other context it would need to be modified.
if you use
onTouchListener
, you don't have to useonClickListener
. inonClickListener
what it does is it get the touch event and check event actions and detect the click. so if you want to do some work whenonClick
. you can do it in theonTouchListener
by filtering the action.I agree with jdx, if you use onTouchlistener you dont need to use onclicklistener and viceversa, if you want to trigger an event on button,image,or textview click, then just fire onClicklistener. if you want some animations like dragging and spinning then use ontouchlistener to get the coordinates of the surface touched
In you GestureDetector, you can call callOnClick() directly. Note the View.callOnClick API requires API level 15. Just have a try.