After many experiments with onTouchEvent and onTouch, I found that onTouch works everywhere you want (whether it is in activity or view) as long as you have declared the interface and put the Listener right! On the other hand, onTouchEvent only works inside a View! Is my assumption correct? Is this the real difference?
onTouchEvent is a method implemented by the View, Activity and other base classes like LinearLayout, etc.. public boolean onTouchEvent(MotionEvent event) { throw new RuntimeException("Stub!" ); }
onTouch. Called when a touch event is dispatched to a view. This allows listeners to get a chance to respond before the target view.
Try code below to detect touch events. mView. setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //show dialog here return false; } }); To show dialog use Activity method showDialog(int).
Yes you are correct - onTouch()
is used by users of the View
to get touch events while onTouchEvent()
is used by derived classes of the View
to get touch events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With