user8062843
user8062843

Reputation: 143

Android: Detect long press on the whole activity

How can i detect a long press on the whole activity? since onLongClickListener is only for individual views.I want to run a method everytime the user longpress the screen

Upvotes: 1

Views: 1334

Answers (2)

Dziugas
Dziugas

Reputation: 1570

You can override your activity's dispatchTouchEvent() method. You also need a gesture detector in order to determine which motion events are 'long presses'. Put this into your activity:

final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
    public void onLongPress(MotionEvent e) {
        // The code for when a long-press happens
    }
});

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    gestureDetector.onTouchEvent(event);
    return super.dispatchTouchEvent(event);
}

Please note that I did not test the above code.

Upvotes: 4

al3xkr
al3xkr

Reputation: 46

A long press is actually multiple registers of the key. So you could do a while loop on the input and as long as input is not NULL run the method you want. If I understood you correctly this should do the trick...

Upvotes: 0

Related Questions