Double-click textview android… here is a solution to the problem.
Double-click textview android
I
want to double-click on the textview because I used the code below
But still not 🙁
TextView txtOne;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtOne = (TextView) findViewById(R.id.txtOne);
txtOne.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("DRAG");
return gestureDetector.onTouchEvent(event);
}
});
}
final GestureDetector gestureDetector = new GestureDetector(
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
System.out.println("Double Tap");
return super.onDoubleTap(e);
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
System.out.println("One Click");
return super.onSingleTapUp(e);
}
});
Only drag invokes, but not double-click and click
Solution
Try the following steps.
Step 1
Write the following code in your activity.
// initialize the Gesture Detector
gd = new GestureDetector(this,new OnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
TODO Auto-generated method stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
TODO Auto-generated method stub
return false;
}
@Override
public boolean onDown(MotionEvent e) {
TODO Auto-generated method stub
return false;
}
});
set the on Double tap listener
gd.setOnDoubleTapListener(new OnDoubleTapListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
Toast.makeText(SplashActivity.this,"Double Tap",Toast.LENGTH_LONG).show();
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
if the second tap hadn't been released and it's being moved
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
TODO Auto-generated method stub
return false;
}
});
Step two
Write the following code for the activity. Here gd
will be the GestureDetector
object.
txt.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
TODO Auto-generated method stub
gd.onTouchEvent(event);
return false;
}
});