Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not get Touch event for TYPE_SYSTEM_OVERLAY

I like to develop an app that is similar to Swapps .i am trying to display a button on top of any screen and that should occur only on a swiping action.I tried with the below code

    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.type =WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
    params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
    params.format =PixelFormat.TRANSLUCENT; 
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
    parentlay = new LinearLayout(this);
    mybt = new Button(getApplicationContext());
    bt.setText("Click to Go");

    parentlay.setBackgroundColor(Color.TRANSPARENT);
    parentlay.setHapticFeedbackEnabled(true);
    parentlay.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
                // event.getAction() prints 4 and not able to get up or down event
           }
       });
   windowmanager.addView(parentlay, params);

I am trying to display the button when user swipes from left-to-right by capturing that event and doing

    parentlay.addView(bt);

and remove the view when user swipes from right-to-left by

    parentlay.removeView(bt);

As mentioned i coudn't get the motion events unless i changed the type as TYPE_SYSTEM_ALERT ,but that inturn freezes all the screen and could not click back or home button,is there is any alternate way to do this.

I have just tried below modification

    params.type =WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
    |WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
   |WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

    params.height = WindowManager.LayoutParams.MATCH_PARENT;
    params.width = WindowManager.LayoutParams.MATCH_PARENT;

But i couldn't click the underneath button ,may be its due to MATCH_PARTENT but if i set it to wrap_content then i cannot get click events.

like image 225
ganesh Avatar asked Feb 18 '13 14:02

ganesh


1 Answers

To hide the view you need to call mWindowManager.removeView(mOverlay);

private RelativeLayout mOverlay;
        private WindowManager mWindowManager;   
     WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                        PixelFormat.TRANSLUCENT);
                params.gravity = Gravity.TOP;
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                mOverlay = (RelativeLayout) inflater
                        .inflate(R.layout.phasechange, null);

                mWindowManager = (WindowManager) context
                        .getSystemService(Context.WINDOW_SERVICE);
                mWindowManager.addView(mOverlay, params);
                Button b = (Button) mOverlay.findViewById(R.id.button1);
                b.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        mWindowManager.removeView(mOverlay);
                    }
                });

            }
like image 178
DjHacktorReborn Avatar answered Oct 20 '22 01:10

DjHacktorReborn