Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Click and Touch Listeners in Android

Tags:

android

I have a bit of doubt. I am using an image button (e.g. Play icon in media player). I want to know which action Listener I am supposed to use, onClickListener or onTouchListener. What is the difference between those two actions and when should I use either.

like image 690
yugandhar babu Avatar asked Nov 08 '11 06:11

yugandhar babu


People also ask

How do I turn off Touch listener on Android?

Use btn. setEnabled(false) to temporarily disable it and then btn. setEnabled(true) to enable it again.

How to handle touch events in Android?

You can react to touch events in your custom views and your activities. Android supports multiple pointers, e.g. fingers which are interacting with the screen. The base class for touch support is the MotionEvent class which is passed to Views via the onTouchEvent() method. you override the onTouchEvent() method.

What is touch listener?

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


3 Answers

The answer by @vishy1618 has the key insight of this thread (tried to leave this as a comment there, but too long).

Conceptually, onClick is just a 'wrapper' around a particular sequence of touch events - down, no drag, up. So comparing onTouch vs. onClick is just a low-level API (raw touch events) vs. a high-level API (a logical user 'click').

But, an important compatibility issue: in Android, onClick can also be fired by the KEYBOARD (or trackball, or whatever alternative input/hardware device is being used). But (afaict) there's no support for firing touch events via any other input device apart from the touch screen.

So, if you code your UI against touch events exclusively, you are implicitly requiring a touchscreen. Whereas if you stick to onClick, your app could theoretically work on a non-touch device.

Of course, all 'compliant' Android phones currently do have touch screens ... so this is effectively moot. But if you want your app to work on non-phone hardware, this might be worth considering.

There is some good discussion here:

How to determine if an Android device has a touchscreen?

https://groups.google.com/forum/?fromgroups=#!topic/android-beginners/cjOVcn0sqLg

like image 150
Mike Repass Avatar answered Oct 24 '22 23:10

Mike Repass


  • onClickListener is used whenever a click event for any view is raised, say for example: click event for Button, ImageButton.

  • onTouchListener is used whenever you want to implement Touch kind of functionality, say for example if you want to get co-ordinates of screen where you touch exactly.

Update:

Just check the official doc for both: onClickListener and onTouchListener.

So from official doc, definition for both are:

  • onClickListner: Interface definition for a callback to be invoked when a view is clicked.
  • onTouchListener: Interface definition for a callback to be invoked when a touch event is dispatched to this view. The callback will be invoked before the touch event is given to the view.
like image 27
Paresh Mayani Avatar answered Oct 24 '22 23:10

Paresh Mayani


The onClickListener is a number of events that are triggered using either the keyboard or the touchscreen. They are performed on a specific view, and the entire view receives the event. In contrast, the onTouchListener is used only for touchscreen events, and they cannot be triggered through the keyboard or any other inputs. They typically also receive the corresponding touch information like the x, y corrdinates, etc.

I think the onClickListener would be appropriate for your application, if you are not using more complex inputs, like gestures, etc.

like image 33
vishy1618 Avatar answered Oct 25 '22 01:10

vishy1618