0

I want to apply a doubleClick event on a text field which displays some text on double click event but it keeps giving me errors is there any method to just simply doing this

TextView tv;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.mytext);
    tv.setOnTouchListener(new OnDoubleTapListener() {
                              @Override
                              public boolean onSingleTapConfirmed(MotionEvent e) {
                                  return true;
                              }

                              @Override
                              public boolean onDoubleTap(MotionEvent e) {
                                  tv.setText("DoubleTouch");
                                  return true;
                              }

                              @Override
                              public boolean onDoubleTapEvent(MotionEvent e) {
                                  tv.setText("Double Touch ");
                                  return true;
                              }
                          }

    );
}
maanijou
  • 1,067
  • 1
  • 14
  • 23

1 Answers1

2

You should initialize the Gesture Detector like this

  GestureDetector gd = new GestureDetector(this, new GestureDetector.OnGestureListener() {
        @Override
        public boolean onDown(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent motionEvent) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent motionEvent) {

        }

        @Override
        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            return false;
        }
    });

Then set on double click listener like this

  gd.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public boolean onDoubleTap(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent motionEvent) {
            return false;
        }
    });

Finally you apply the listener to your View like this

  tv.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        gd.onTouchEvent(event);
        return false;
    }
});


Refer Android docs for more info Detect Common gestures

Mangaldeep Pannu
  • 3,738
  • 2
  • 26
  • 45
Amine
  • 2,241
  • 2
  • 19
  • 41