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.
Use btn. setEnabled(false) to temporarily disable it and then btn. setEnabled(true) to enable it again.
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.
Called when a touch event is dispatched to a view. This allows listeners to get a chance to respond before the target view.
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
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.
Just check the official doc for both: onClickListener and onTouchListener.
So from official doc, definition for both are:
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.
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