1

I'm trying to create an overlay window using the next code:

int OVERLAY_TYPE;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    OVERLAY_TYPE = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
    OVERLAY_TYPE = WindowManager.LayoutParams.TYPE_PHONE;
}

WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

ImageView popupView = new ImageView(this);
popupView.setImageResource(R.drawable.bottom_dialog_background);

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        OVERLAY_TYPE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);

params.gravity = Gravity.BOTTOM;

windowManager.addView(popupView, params);

I've added <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> to Manifest and allowed all needed permissions for that.

It works fine for Android versions up to Android Q. But Q and R versions don't work at the same way. Overlay window creates, persists through some activities and other apps. It even persists through Android Settings but when I try to open something deeper than just Android Settings, for example Usage Data Access screen or App Data screen, the Overlay Window disappears. If I press Back button it will show the previous Activity and Overlay Window will be shown again.

Does anyone know how to "fix" it and achieve the same behavior for all Android versions? Any help would be highly appreciated. Thanks in advance.

Oleg Golomoz
  • 502
  • 4
  • 12
  • Did you ever get the answer for this ? if so please help have been searching for week but i cant get it to work ;'( – Ahsan raza Feb 26 '21 at 08:32
  • Have similar issue. Always get `permission denied for window type 2038` when trying to addView() in Foreground Service, on Android 10 and 11. Changing to `TYPE_APPLICATION_PANEL` gave another error. – user1801605 Jul 20 '21 at 04:15
  • Also have a look at my question https://stackoverflow.com/q/76651691/4134215 – Taimoor Khan Jul 16 '23 at 20:10

0 Answers0