Search code examples
androidtouchgesture

Detect gesture over everthing


How can I detect any gesture in my Activity, even with many things there (Layouts, Images, RecyclerViews, etc). I intent to create an gesture that can be done at any time. Is it possible?

I've tried this:

findViewById(android.R.id.content).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.e("GESTURE", "GESTURE");
                return false;
            }
        });

But it's not working. I think it's because the others views is intercepting the event.

Idk if is it possible to detect the gesture above the activity, like in the window.

Thank you!


Solution

  • You can, but it's a bit tricky. Read up on touch handling and especially onInterceptTouchEvent, which can be used on a parent to intercept touches going to the children.

    You would implement that method in a (custom) root view, and then do your gesture magic in there.

    Some valuable info here: Android: Difference between onInterceptTouchEvent and dispatchTouchEvent?