Android Create System Overlay Window like Facebook Chat Heads

For floating overlay window  in Android we need following permission on “AndroidManifest.xml”

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

How to open overlay window/ How get permission to draw overlay screen:

 public final static int Overlay_REQUEST_CODE = 251;
    public void checkDrawOverlayPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.canDrawOverlays(mActivity)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, Overlay_REQUEST_CODE);
            } else {
                openFloatingWindow();
            }
        } else {
            openFloatingWindow();
        }
    }

    private void openFloatingWindow() {
            Intent intent = new Intent(mActivity, FloatingWindow.class);
            mActivity.stopService(intent);
            mActivity.startService(intent);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case Overlay_REQUEST_CODE: {
                if (Build.VERSION.SDK_INT >= 23) {
                    if (Settings.canDrawOverlays(mActivity)) {
                        openFloatingWindow();
                    }
                } else {
                    openFloatingWindow();
                }
                break;
            }
        }
    }

We need to call checkDrawOverlayPermission() function to open overlay/floating window.

We need following functions to get focus on EditText of floating screen (Window on top of all applications)

   private void editTextReceiveFocus() {
        if (!wasInFocus) {
            mWindowsParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
            mWindowManager.updateViewLayout(mView, mWindowsParams);
            wasInFocus = true;
        }
    }

    private void editTextDontReceiveFocus() {
        if (wasInFocus) {
            mWindowsParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
            mWindowManager.updateViewLayout(mView, mWindowsParams);
            wasInFocus = false;
        }
    }

You can download complete source code with example form GitHub

Android System Overlay Window like Facebook Chat Heads

This overlay window is used in English Urdu Dictionary for Android smart phones.

Android floating window

Leave a comment

Your email address will not be published. Required fields are marked *