Difference Between Ontouch and Onclick Android

Difference between OnTouch and OnClick Android

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.

Difference between Click and Touch Listeners in Android

  • 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.

difference between onTouch and onClick listener and use of multithreading

  1. For the click event, keep using onClick, onTouch is used for tracking gestures.

  2. It's not a good practice to do any logic operation inside the Main Thread. Leave it just for user interaction. You should consider use threading to avoid ANR (Application not responding). A good start is AsyncTask, that has it own method to do logic in a separete thread and update the UI on the Main Thread.

Please follow this link to get help with AsyncTask: https://stackoverflow.com/a/18827536/4973904

Hope that this could help you!

Difference between OnTouchListener and OnClickListener

OnTouchListener is for more fine-grained control than OnClickListener. If what you really care about is clicks -- i.e., the combination of of down-touch/up-touch/no-drag -- then use OnClickListener, that's its purpose.

Android | What is the difference between setOnClickListener and setOnTouchListener?

OnTouch is Motionevent. You can drag up,down,left and right. But OnClick is simply focusing,pressing and releasing. When you want to get co-ordinates of screen where you touch the screen, use touchlistener. Just a click event for button and imageview,use OnClickListener.

Set BOTH, android:onClick AND OnTouchListener

Yes you can use both onClick and onTouch on a same button, but OnTouch callback you'll get motionEvent like ACTION_MOVE, ACTION_UP , ACTION_DOWN etc, Don't forget to return false (Details)in onTouch callback. Please refer the below code

Button button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
Log.d("test", "ontouch");
return false;
}
});
button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Log.d("test", "onclick");
}

});

Just do the operations you want to do in the call backs onTouch and onClick respectively.
Please NOte click is a action performed when user press the button and release but Touch will be taken when user presses it.

So on a single click the log will be like this. 1.ACTION_DOWN, 2.ACTION_UP 3. onClick

03-22 16:19:39.735: D/test(682): ontouch
03-22 16:19:39.735: D/test(682): ontouch
03-22 16:19:39.735: D/test(682): onclick

how to use both Ontouch and Onclick for an ImageButton?

Try this, It may help you

No need to set onClick() method onTouch() will handle both the case.

package com.example.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;

public class MainActivity extends Activity {
private GestureDetector gestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetector(this, new SingleTapConfirm());
ImageButton imageButton = (ImageButton) findViewById(R.id.img);

imageButton.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {

if (gestureDetector.onTouchEvent(arg1)) {
// single tap
return true;
} else {
// your code for move and drag
}

return false;
}
});

}

private class SingleTapConfirm extends SimpleOnGestureListener {

@Override
public boolean onSingleTapUp(MotionEvent event) {
return true;
}
}

}

OnClick and onTouch

There is no need for you to use the onClick event, since you can easily capture the click using the onTouch callback. A click is a sequence of one ACTION_DOWN action, several ACTION_MOVE actions and one ACTION_UP action. These can be acquired using the event.getAction() method. If you get an ACTION_DOWN event and then an ACTION_UP event - it means that the user has just clicked your View. You can also measure time spent between these events to be sure that it was a simple click, not a long one. And, of course, you can use the event.getX() and event.getY() methods to get the exact touch point. Hope this helps.

OnClickListener and OnTouchListener collision issues

I'd suggest you use only the OnTouchListener and handling the click case there using something like:

    ImageButton floatingIcon = (ImageButton) findViewById(R.id.floatingIcon);
gestureDetector = new GestureDetector(this, new YourGestureListener());
floatingIcon.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (gestureDetector.onTouchEvent(arg1)) {
// A single tap has been made - treat it like a click and consume the event
return true;
} else {
// The MotionEvent is not a click event, handle and decide if the event is consumed
}
return false;
}
});

private class YourGestureListener extends SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent event) {
return true;
}
}


Related Topics



Leave a reply



Submit