1

I'm trying to detect a long press in Android. GestureDetector is deprecated, so I was trying to use Handlers. But handler isn't recognizing postDelayed or removeCallbacks. It Cannot resolve method for either.

    final Handler handler = new Handler() {
        @Override
        public void publish(LogRecord record) {

        }

        @Override
        public void flush() {

        }

        @Override
        public void close() throws SecurityException {

        }
    };

    Runnable longPressed = new Runnable() {
        @Override
        public void run() {
            Log.d("run", "long pressed");
        }
    };

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                handler.postDelayed(longPressed, 500);
                break;
            case MotionEvent.ACTION_MOVE:
                handler.removeCallbacks(longPressed);
                break;
            case MotionEvent.ACTION_UP:
                handler.removeCallbacks(longPressed);
                break;
        }

        return super.onTouchEvent(event);
    }
}
stumped
  • 3,235
  • 7
  • 43
  • 76
  • 2
    Since the `GestureDetector` class is not deprecated, why not use `GestureDetector` and avoid the deprecated constructors? https://developer.android.com/reference/android/view/GestureDetector.html – CommonsWare Mar 08 '18 at 20:21

3 Answers3

1

What about View.OnLongClickListener.html?


You would get something like:

yourView.setOnLongClickListener(new View.OnLongClickListener() {
  @Override public boolean onLongClick(View v) {
    // Toast it out!
    return false;
  }
});
Evin1_
  • 12,292
  • 9
  • 45
  • 47
0

GestureDetector is deprecated is not exactly true.
Only the ones Not including Context as constructor parameter are deprecated. Others with context work fine.

final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
    public void onLongPress(MotionEvent e) {
        Log.e("", "Longpress detected");
    }
});

public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
};
Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
0

You can use Handler if you don't want to use Gesture Detector.

//Declare this flag globally
boolean goneFlag = false;
//Put this into the class
final Handler handler = new Handler(); 
Runnable mLongPressed = new Runnable() { 
    public void run() { 
        goneFlag = true;
        //Code for long click
    }   
};

//onTouch code
@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {    
    case MotionEvent.ACTION_DOWN:
        handler.postDelayed(mLongPressed, 1000);

        break;
    case MotionEvent.ACTION_UP:
        handler.removeCallbacks(mLongPressed);
        if(Math.abs(event.getRawX() - initialTouchX) <= 2 && !goneFlag) {
            //Code for single click
            return false;
        }
        break;
    case MotionEvent.ACTION_MOVE:
        handler.removeCallbacks(mLongPressed);

        break;
    }
    return true;
}
Chetan Mehra
  • 189
  • 1
  • 3
  • 20