0

I am currently trying to detect an ongoing touch event in my Android app. In detail I want to recognize within a fragment whether the user touches any part of the app's screen.

Android's OnTouchListener works as expected except if the touch event lasts longer than a few seconds without moving.

For example the first 1-2 seconds of touch are being detected but everything after won't.

Is there something like an "OnPressListener" or a workaround?

Markus
  • 1,649
  • 1
  • 22
  • 41

3 Answers3

0

If it's a well defined gesture you are trying to detect, have a look at GestureDetector.

http://developer.android.com/reference/android/view/GestureDetector.html

Michael Krause
  • 4,689
  • 1
  • 21
  • 25
0

You can use

   aButton.setOnLongClickListener(new OnLongClickListener() {
    public boolean onLongClick(View arg0) {
        Toast.makeText(getApplicationContext(), "Long Clicked " ,
              Toast.LENGTH_SHORT).show();

        return true;    // <- set to true
    }
    });

on your aButton and if you are using API < 19 you have to add

android:longClickable="true"

Attribute to your aButton in layout xml.

GioLaq
  • 2,489
  • 21
  • 26
  • Thanks for your answer, but I need to get an event as long as the touch lasts, not only an event once the touch occured. – Markus Oct 09 '14 at 19:57
0

I finally found it out.

The solution is to use a simple OnTouchListener:

private boolean pressed = false;

@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    if (action == MotionEvent.ACTION_DOWN) {
        pressed = true;

    } else if ((action == MotionEvent.ACTION_UP)
            || (action == MotionEvent.ACTION_CANCEL)) {
        pressed = false;
    }

    return true;
}
Markus
  • 1,649
  • 1
  • 22
  • 41