In my activity only Long click listener is working fine but not touch event. How can we use both events on same textview.
textView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Toast.makeText(Reminder.this,"press long on the screen", Toast.LENGTH_LONG).show();
return false;
}
});
code for swiping event
@SuppressLint("SetTextI18n")
public boolean onTouchEvent(MotionEvent touchEvent) {
switch (touchEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = touchEvent.getX();
break;
case MotionEvent.ACTION_UP:
x2 = touchEvent.getX();
if (x1>x2){
//do something
return false;
}
if (x1 < x2) {
//do something
return false;
}
break;
}
return true;
}
CodePudding user response:
just add this code in your onCreate
method . It will for both setOnLongClickListener
and swiping events.
final GestureDetector gesturedt= new GestureDetector(Reminder.this, new GestureDetector.SimpleOnGestureListener(){
//method for double tap
@Override
public boolean onDoubleTap(MotionEvent e) {
//your action here for double tap e.g.
//Log.d("OnDoubleTapListener", "onDoubleTap");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
// here is your long click event
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return true;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
});
textView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
if (x1>x2){
//do something
}
if (x1 < x2) {
//do something
}
return false;
}
break;
}
return gesturedt.onTouchEvent(event);
}
});
CodePudding user response:
So it's possible the onTouchEvent
listener is overriding what would normally get caught in the onLongClick
. If the onTouchEvent still works, I'd suggest just including code there to handle long clicks as well.
For example:
textView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() ==
MotionEvent.ACTION_DOWN) {
long eventDuration = event.getEventTime() - event.getDownTime();
if (eventDuration > 2000) {
if (gestureDetector.onTouchEvent(event)) {
//Do something for long clicks here;
}
}
}
return false;
}
});