93

Is there any difference between OnTouchListener and OnClickListener? I am not asking from programming point of view, but from User Experience point of view.

Which one is better to use?

Do we need to implement both?

Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91
Nixit Patel
  • 4,435
  • 5
  • 30
  • 57

6 Answers6

107

which one is better to use?

It really depends on your requirement.

onTouch gives you Motion Event. Thus, you can do a lot of fancy things as it help you separate state of movement. Just to name a few:

  • ACTION_UP
  • ACTION_DOWN
  • ACTION_MOVE

Those are common actions we usually implement to get desire result such as dragging view on screen.

On the other hand, onClick doesn't give you much except which view user interacts. onClick is a complete event comprising of focusing,pressing and releasing. So, you have little control over it. One side up is it is very simple to implement.

do we need to implement both?

It is not necessary unless you want to mess up with your user. If you just want simple click event, go for onClick. If you want more than click, go for onTouch. Doing both will complicate the process.

From User point of view, it is unnoticeable if you implement onTouch carefully to look like onClick.

Lii
  • 11,553
  • 8
  • 64
  • 88
PH7
  • 3,926
  • 3
  • 24
  • 29
  • 3
    If you need some control then go for onTouch. For most of the cases onClick will do. See PH7's answer. – drulabs Feb 03 '12 at 04:28
  • I'm still trying to figure out which to use. If I have 4 views, 3 of which require both a click and a swipe while the 4th only a click. I know I can use onTouch only as clicks or swipe for the first 3 views but what about the 4th? Should I implement onClick for the one view or just use onTouch since it's already called..? – cjayem13 Oct 17 '14 at 16:00
  • Should be noted it may be necessary to use onTouch to achieve certain results, in which case user point of view be damned. FE, if you want to block the soft keyboard from showing, so you can provide your own, it must be via onTouch. TIL onClick won't do it. – ChiefTwoPencils Dec 24 '14 at 01:26
25

A "Touch" event is when someone puts their finger on the screen. It gets called throughout the movement of the finger, down, drag, and up. A "Click" need not even come from the screen. It could be someone pressing the enter key.

Use OnTouchListener when you want to receive events from someone's finger on the screen.

Use OnClickListener when you want to detect clicks.

Thomas Dignan
  • 7,052
  • 3
  • 40
  • 48
  • I have seen some developer using touch event for the button, what will be the reason for that? I haven't tested on the phone but, is there any performance improvisation? – Nixit Patel Feb 03 '12 at 02:17
  • 1
    @NixitPatel No good reason comes to mind without seeing their source. – Thomas Dignan Jun 03 '12 at 23:16
  • true, but now I found my answer... with the help of you guys and some particle approach. no need to worry, Thanks for asking – Nixit Patel Jun 04 '12 at 04:51
9
  • 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.

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.
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • 1
    I have seen some developer using touch event for the button what will be the reason for that? I haven't tested on the phone but, is there any performance improvisation? – Nixit Patel Feb 03 '12 at 02:16
  • what if i want to implement user pressing the joypad mimics on android? Is that touch also instead of click? But it will effecting the pressed call state... (sorry, if any in android) @Lucifer – gumuruh Jan 14 '23 at 09:31
8

One good reason to use ontouch events over onclick events is to get a faster response upon the user click. Where the onclick event suffers a delay in responding and proceeding with required action. ontouch events are better for delivering high performing mobile web apps.

Bassel Mourjan
  • 3,604
  • 3
  • 26
  • 37
2

OnClickListener - is used for a basic purpose, when we just need a click from a user i.e., click of a button, no drag or gestures on the screen

OnTouchListener is basically a more fine-tuned control than OnClickListener. It performs and action on a press/ release on the screen It allows user with combination of move,down-touch,up-touch,finger drag or any movement gesture on the screen

Rohit Goyal
  • 550
  • 8
  • 9
1

Prefer to use onTouch. Give you an example: when you have a ViewPager and a TextView in it. if you implement setOnClickListener on textView, you will not be able to swipe the ViewPager when you touch/swipe on the TextView

LiangWang
  • 8,038
  • 8
  • 41
  • 54