Issue
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;
}
Solution
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);
}
});
Answered By - Rishi Ff
Answer Checked By - Timothy Miller (JavaFixing Admin)