OnTouchevent () vs onTouch ()

After many experiments with onTouchEvent and onTouch, I found that onTouch works wherever you want (whether in action or in a view), until you declare the interface and correctly install the Listener! OnTouchEvent, on the other hand, only works inside the view! Is my assumption correct? Is that the real difference?

+54
android ontouchlistener ontouchevent
Feb 15 2018-11-11T00:
source share
7 answers

Yes, you're right - onTouch() used by View users to receive touch events, and onTouchEvent() used by derived View classes to receive touch events.

+37
Feb 15 2018-11-11T00:
source share

I had some confusion about how onTouchEvent () and onTouch () work (you can see my comment on this question). After some research below, I found out about this. This may be useful for beginners.

1) Implementation:

If you want to use onTouch (), you need to do three things.

1- implement OnTouchListener

2- call setOnTouchListener () in the view you want to set in order to catch the event

3- override onTouch () to handle the event

but if you want to use onTouchEvent (), you do not need to do steps 1 and 2 above. you just need to override onTouchEvent ().

2) Work:

onTouch () works in the view, view, activity mode. This means that you can use onTouch () inside a view, viewgroup or activity. These methods take two arguments [onTouch (View v, MotionEvent e)]. This allows you to filter events for different views in an action or view group. Or the activity itself can handle it. onTouchEvent () takes one argument [ onTouchEvent(MotionEvent e) ]. Thus, this can only be used inside a view that implements it or in a derived form. The derived view allows you to extend the touch behavior defined in onTouchEvent ().

I think that such options are part of a more flexible Android development philosophy, although sometimes this creates confusion for students.

+23
Apr 13 '14 at 13:27
source share

I used ontouch () and ontouchevent (), since ontouch is used when I want to work with elements of a single kind, such as buttons, image buttons, etc. on one image (say Linringayout), whereas when I want to work on areas of the rest of the elements (like a button) I use ontouchevent.

+5
Mar 02 '13 at 11:02
source share

In fact, onTouchEvent () will be called by the action if none of the views accepts the touch event.

And, as you say, onTouch () can be used in any class if:

  • This class (i.e. Foo) implements the OnTouchListener interface and
  • This class is a registered listener using view.setOnTouchListener(foo);
+4
Feb 18 '11 at 7:29
source share

When creating a custom view, you can

@override onTouchEvent(MotionEvent e){}

while you can add onTouch to any View, ViewGroup or Activity.

onTouch(View v, MotionEvent e) { //you can filter any View touch }

onTouch is generic, and onTouchEvent is for viewing. And you can also filter your presentation using onTouch.

+4
Dec 16 '14 at 7:28
source share

I found another difference. onTouchEvent does not seem to receive the deprecated MotionEvent.ACTION_POINTER_2_DOWN and MotionEvent.ACTION_POINTER_2_UP events.

Of course, they are quite old, and we should not use them.

This is in Android 5.1 api 22.

0
Jan 31 '17 at 16:39
source share

onTouchEvent is a method implemented by View , Activity, and other base classes such as LinearLayout , etc. ..

 public boolean onTouchEvent(MotionEvent event) { throw new RuntimeException("Stub!"); } 

you can override this method with any derived classes

then

onTouch () is defined by the OnTouchListener {} interface

public interface OnTouchListener { boolean onTouch(View var1, MotionEvent var2); }

so you only need to implement it when setting this interface to the class

0
Jun 03 '17 at 11:50
source share



All Articles