kevdliu
kevdliu

Reputation: 1819

Android overlay layout on top of all windows that receives touches

I have created a view that displays on top of all applications and windows with the following code:

        //These three are our main components.
    WindowManager wm;
    LinearLayout ll;
    WindowManager.LayoutParams ll_lp;

    //Just a sample layout parameters.
    wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    ll_lp = new WindowManager.LayoutParams();
    ll_lp.format = PixelFormat.TRANSLUCENT;
    ll_lp.height = WindowManager.LayoutParams.FILL_PARENT;
    ll_lp.width = WindowManager.LayoutParams.FILL_PARENT;
    ll_lp.gravity = Gravity.CLIP_HORIZONTAL | Gravity.TOP;

    //This one is necessary.
    ll_lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

    //Play around with these two.
    ll_lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
    ll_lp.flags = ll_lp.flags | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

    //This is our main layout.
    ll = new LinearLayout(this);
    ll.setBackgroundColor(android.graphics.Color.argb(50, 255, 255, 255));
    ll.setHapticFeedbackEnabled(true);
    ll.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Toast.makeText(getApplicationContext(), "TOUCHED", Toast.LENGTH_SHORT).show();
            return false;
        }
        });

    //And finally we add what we created to the screen.
    wm.addView(ll, ll_lp);

Because FLAG_NOT_TOUCHABLE is set, it only displays the view but doesn't receive any touch events. The application behind the view receives all touch events. However, if i don't set the flag, then only the view receives touches causing the application behind it to not receive any.

Is there a way for both the view and the application behind it to receive touches? I have tried returning false but still the same.

Any help would be greatly appreciated!

Upvotes: 7

Views: 8816

Answers (1)

Akos Cz
Akos Cz

Reputation: 12790

If im not mistaking, the view thats in the background is not receiving touch events because its being filtered out by the system to prevent "click jacking" exploits.

You might be able to get around this system feature by turning off the filtering of touch events for the view in the background using the View.setFilterTouchesWhenObscured(Boolean) method.

Upvotes: 2

Related Questions