Android: is there a way to apply android:filterTouchesWhenObscured="true" on every layout?

Viewed 278

I want to detect and act upon overlays obscuring my App.

I have the following code to decide what to do:

final View.OnTouchListener filterTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
                Toast.makeText(v.getContext(), "Full overlay detected", Toast.LENGTH_SHORT).show();
                return false;
            }
            if ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_PARTIALLY_OBSCURED) != 0) {
                Toast.makeText(v.getContext(), "Partial overlay detected", Toast.LENGTH_SHORT).show();
                return false;
            }
            return false;
        }
    };
    view.setOnTouchListener(filterTouchListener);

I tried to apply and inherit the following in onCreate() to apply "filterTouchesWhenObscured" to every Activity:

View view = findViewById(android.R.id.content).getRootView();

view.setFilterTouchesWhenObscured(true);

But I get different behaviors if I add it via Java or if I add it to the layout;

The programmatic solution blocks all touch events and does not trigger the OnTouchListener.

The layout solution allows me to use the OnTouchListener, but needs to be applied to every layout manually.

Is there a way to get the behavior of android:filterTouchesWhenObscured="true" without adding it manually to every single layout?

0 Answers
Related