Offensive
Offensive

Reputation: 43

Android - How do you make a button on a screen overlay clickable

I am trying to implement a simple lockscreen through screen overlay. I added a button to the XML layout, the app successfully covered the screen but the button doesn't seem to work.

    public WindowManager winManager;
    public RelativeLayout wrapperView;

    @Override
    protected void onCreate(Bundle savedInstanceState). {      ((KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock("IN").disableKeyguard();
    PackageManager p = getPackageManager();
            p.setComponentEnabledSetting(getComponentName(),     PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        finish();
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams     localLayoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|

                                                                                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
                                                                                      WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                                                                                      PixelFormat.TRANSLUCENT);

        this.winManager =     ((WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE));
        this.wrapperView = new     RelativeLayout(getBaseContext());
        getWindow().setAttributes(localLayoutParams);
        View.inflate(this, R.layout.lackiscreen, this.wrapperView);
        this.winManager.addView(this.wrapperView, localLayoutParams);


    }
    public void dclick(View view){
        this.winManager.removeView(this.wrapperView);
        this.wrapperView.removeAllViews();
    }

Upvotes: 3

Views: 1943

Answers (1)

Johey
Johey

Reputation: 275

Android only allows very specific windows to receive touch events when they are an overlay. You need to get your flags right.

WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
);

Hope that helps. (Works for me at least)

Upvotes: 4

Related Questions