onTouchevent() vs onTouch()

AndroidOntouchlistenerTouch Event

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

I had some confusion related to how onTouchEvent() and onTouch() work (You can see my comment to this question). After some research below is what I have found on it. This may be helpful to beginners.

  1. Implementation:

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

1- implement OnTouchListener

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

3- override onTouch() to handle the event

but if you want to use onTouchEvent() you don't need to do step 1 & 2 above. just you need to override onTouchEvent().

  1. Working:

    onTouch() works on view, viewgroup, activity. Meaning you can use onTouch() inside view, viewgroup or activity. This methods take two arguments [onTouch(View v, MotionEvent e) ]. This allows you filter events for different views in an activity or view group. Or the activity can itself handle it. onTouchEvent() takes on one argument [onTouchEvent(MotionEvent e) ]. Thus this can be used only inside the view that implements it or on the derived view. A derived View enables extension of the touch behavior defined in onTouchEvent().

I think, such options are part of Android's more flexible development philosophy, though it creates confusion for the learners at times.

Solution 3 - Android

I have used ontouch() and ontouchevent(), as ontouch is used when i want to work on elements of single view, like buttons , imagebuttons etc on single view (say Linearlayout ), whereas when i want to work on areas rest of the my elements(e.g button) i use ontouchevent.

Solution 4 - Android

The onTouchEvent() actually will get called by the Activity if none of the views consume the touch event.

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

  1. That class (i.e. Foo) implements the OnTouchListener interface and
  2. That class is a registered listener using view.setOnTouchListener(foo);

Solution 5 - Android

While creating a custom View ,you can

@override onTouchEvent(MotionEvent e){}

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

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

onTouch is generic and onTouchEvent is specific to View. And you can also filter your view using onTouch.

Solution 6 - Android

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

Of course they are quite old and we shouldn't be using them.

This is in Android 5.1 api 22.

Solution 7 - Android

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

you can override this method by any derived classes

whereas

onTouch() is defined by the interface OnTouchListener{}

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

so you only need to implement one when setting this interface to a class

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsteliosView Question on Stackoverflow
Solution 1 - AndroidtrojanfoeView Answer on Stackoverflow
Solution 2 - AndroidDexterView Answer on Stackoverflow
Solution 3 - AndroidhemantsbView Answer on Stackoverflow
Solution 4 - AndroidpaiegoView Answer on Stackoverflow
Solution 5 - AndroidZar E AhmerView Answer on Stackoverflow
Solution 6 - AndroidDave HubbardView Answer on Stackoverflow
Solution 7 - AndroidgorView Answer on Stackoverflow