Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use both Ontouch and Onclick for an ImageButton?

Tags:

android

in my app, i want two things to happen.

  1. when i touch and drag the ImageButton, it should move along with my finger.

    i used OnTouchListener() for this and it works fine.

  2. when i click the ImageButton, it should close the activity.

    i used OnClickListener() for this and it also works fine.

So, here is my problem. whenever i move the ImageButton OnTouchListener is tirggered and the ImageButton moves, the OnClickListener is also triggered at the end when i am releasing the button from moving.

How to use ontouch and onclick listeners on the same button without interfering on each other?

like image 450
Praveen Thamizhazhagan Avatar asked Oct 23 '13 10:10

Praveen Thamizhazhagan


People also ask

How many ways can you attach OnClickListener to button layout?

Using an OnClickListener There are two ways to do this event handler programmatically : Implementing View. OnClickListener in your Activity or fragment.

How do I use onTouchEvent on Android?

After the Math. abs() calls, you're essentially testing if their finger is farther down the screen than it is across the screen. Store the initial down coordinates as member variables and set them during ACTION_DOWN . You declared two floats (touchX and touchY) inside the onTouchEvent method.

What is onClick listener?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.

What is onTouch?

onTouch. Added in API level 1. public abstract boolean onTouch (View v, MotionEvent event) Called when a touch event is dispatched to a view. This allows listeners to get a chance to respond before the target view.


2 Answers

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;         }     }  } 
like image 62
Biraj Zalavadia Avatar answered Sep 21 '22 23:09

Biraj Zalavadia


To have Click Listener, DoubleClick Listener, OnLongPress Listener, Swipe Left, Swipe Right, Swipe Up, Swipe Down on Single View you need to setOnTouchListener. i.e,

view.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {              @Override             public void onClick() {                 super.onClick();                 // your on click here             }              @Override             public void onDoubleClick() {                 super.onDoubleClick();                 // your on onDoubleClick here             }              @Override             public void onLongClick() {                 super.onLongClick();                 // your on onLongClick here             }              @Override             public void onSwipeUp() {                 super.onSwipeUp();                 // your swipe up here             }              @Override             public void onSwipeDown() {                 super.onSwipeDown();                 // your swipe down here.             }              @Override             public void onSwipeLeft() {                 super.onSwipeLeft();                 // your swipe left here.             }              @Override             public void onSwipeRight() {                 super.onSwipeRight();                 // your swipe right here.             }         });  } 

For this you need OnSwipeTouchListener class that implements OnTouchListener.

public class OnSwipeTouchListener implements View.OnTouchListener {  private GestureDetector gestureDetector;  public OnSwipeTouchListener(Context c) {     gestureDetector = new GestureDetector(c, new GestureListener()); }  public boolean onTouch(final View view, final MotionEvent motionEvent) {     return gestureDetector.onTouchEvent(motionEvent); }  private final class GestureListener extends GestureDetector.SimpleOnGestureListener {      private static final int SWIPE_THRESHOLD = 100;     private static final int SWIPE_VELOCITY_THRESHOLD = 100;      @Override     public boolean onDown(MotionEvent e) {         return true;     }      @Override     public boolean onSingleTapUp(MotionEvent e) {         onClick();         return super.onSingleTapUp(e);     }      @Override     public boolean onDoubleTap(MotionEvent e) {         onDoubleClick();         return super.onDoubleTap(e);     }      @Override     public void onLongPress(MotionEvent e) {         onLongClick();         super.onLongPress(e);     }      // Determines the fling velocity and then fires the appropriate swipe event accordingly     @Override     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {         boolean result = false;         try {             float diffY = e2.getY() - e1.getY();             float diffX = e2.getX() - e1.getX();             if (Math.abs(diffX) > Math.abs(diffY)) {                 if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {                     if (diffX > 0) {                         onSwipeRight();                     } else {                         onSwipeLeft();                     }                 }             } else {                 if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {                     if (diffY > 0) {                         onSwipeDown();                     } else {                         onSwipeUp();                     }                 }             }         } catch (Exception exception) {             exception.printStackTrace();         }         return result;     } }  public void onSwipeRight() { }  public void onSwipeLeft() { }  public void onSwipeUp() { }  public void onSwipeDown() { }  public void onClick() {  }  public void onDoubleClick() {  }  public void onLongClick() {  } } 
like image 29
Jaydipsinh Zala Avatar answered Sep 18 '22 23:09

Jaydipsinh Zala