Search code examples
androidandroid-windowmanager

In Floating window not able to get listview clicks events for background apps


I have created Floating button am able to keep on required position as shown in image as well access other apps as i launched settings app. but it is blocking back key and list view item click for background apps as shown in image if we consider wifi option am able to turn button on or off but not able to go inside wifi option. please help solving this problem

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        show(0);
        return START_NOT_STICKY;
    }
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);
    private int width;
    private int height;
    public final synchronized void show(final int id) {
        width = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
        height = ((Activity) context).getWindowManager().getDefaultDisplay().getHeight();
        final Button button = new Button(context);
        button.setText("I'm a button!");
        button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        button.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.performClick();

                if(event.getAction() == MotionEvent.ACTION_MOVE){
                    int pos[] = new int[2];
                    button.getLocationOnScreen(pos);
                    Log.i(MainActivity.TAG, "onTouch"+event.getRawX()+" :: "+width+" :: "+pos[0]);
                    Log.d(MainActivity.TAG, "onTouch"+event.getRawY()+" :: "+height+" :: "+pos[1]);
                    params = new WindowManager.LayoutParams(
                            WindowManager.LayoutParams.WRAP_CONTENT,
                            WindowManager.LayoutParams.WRAP_CONTENT,
                            (int)(event.getRawX())-(width/2), 
                            (int)(event.getRawY())-(height/2),
                            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                            PixelFormat.TRANSLUCENT);


                    mWindowManager.updateViewLayout(button, params);
                                }
                return true;
            }
        });
        mWindowManager.addView(button, params);
    }

enter image description here


Solution

  • Need to add flags 0 and WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE then it will work fine as bellow snippet

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.performClick();
            if(event.getAction() == MotionEvent.ACTION_MOVE){
                params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        (int)(event.getRawX())-(width/2), 
                        (int)(event.getRawY())-(height/2),
                        0,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                        PixelFormat.TRANSLUCENT);
                mWindowManager.updateViewLayout(floatingView, params);
                return true;
            }
            return true;
        }