I have a TextView with a swipe listener on it. I wanted to add a long click listener as well. They both work but when I swipe, the long click listener is triggered as well, which is not what I want. I need them to be independent. I've found some very old code about detecting long press but it's not very clear what's the best approach.
here's what I have in my MainActivity
tvIta.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
@Override
public void onSwipeRight() {changeSays(); }
@Override
public void onSwipeLeft() { changeSays(); }
});
tvIta.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
String label = "Label_Ita";
String text = tvIta.getText().toString();
ClipboardManager clipboard = (ClipboardManager) getSystemService(MainActivity.this.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
Toast.makeText(MainActivity.this, "Copied to clipboard!", Toast.LENGTH_SHORT).show();
return true;
}
});
this is the swipe listener class
public class OnSwipeTouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
// commented out otherwise won't work
// @Override
// public boolean onDown(MotionEvent e) {
// return true;
// }
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
Thank you!