Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onTouchevent() vs onTouch()

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?

like image 801
stelios Avatar asked Feb 15 '11 09:02

stelios


People also ask

What is onTouchEvent?

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!" ); }

What is onTouchEvent in Android Studio?

onTouch. Called when a touch event is dispatched to a view. This allows listeners to get a chance to respond before the target view.

How do I get touch event on android?

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).


1 Answers

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.

like image 179
trojanfoe Avatar answered Sep 20 '22 00:09

trojanfoe