9

How to Overlay screen?

  1. it should be touchable
  2. view must overlay system action bar
  3. view must overlay soft buttons back/home/recent_apps
  4. Android 4.0 +

    public class MyService extends Service {
    
    @Override
    public void onCreate() {
        super.onCreate();
        WindowManager.LayoutParams params = ViewUtils.generateFullScreenParams(true);
    
        final WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(overlayView, params);
    }
    ...
    }
    

and layout params creator

public static WindowManager.LayoutParams generateFullScreenParams() {
    return new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,

            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,

            PixelFormat.TRANSLUCENT);
}

ok, looks good.

Flag TYPE_SYSTEM_ALERT not overlays the system bar (android < 5.0), not overlays soft buttons, but I can to handle onTouch event

Flag TYPE_SYSTEM_OVERLAY overlays the system bars, not overlays buttons and I can't to handle onTouch event.

Any ideas?

Abdelilah El Aissaoui
  • 4,204
  • 2
  • 27
  • 47
VKDev
  • 604
  • 7
  • 18
  • Why you net setting theme FullScreen with no titlebar? – IshRoid Feb 27 '15 at 10:22
  • this view attached from service, but theme actual for an activities. Сorrect me if I'm wrong – VKDev Feb 27 '15 at 10:35
  • You creating View inside Service inside OnCreate ?? like dialog? – IshRoid Feb 27 '15 at 10:38
  • maybe I can to detect touch on screen event without view? (for all applications) – VKDev Feb 27 '15 at 11:40
  • No you can not do that. Again what you can do is use system alert below M and take users concern from pendingintent notification with addaction buttons and the run your service instead of opening an activity for performing the task related to users input in background – sandhya sasane Sep 17 '18 at 17:47
  • Does this answer your question? [Draw Overlay in Android (system wide)](https://stackoverflow.com/questions/13346111/draw-overlay-in-android-system-wide) – Gk Mohammad Emon Sep 15 '20 at 04:18

2 Answers2

1
public static WindowManager.LayoutParams generateFullScreenParams() {
    return new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,

            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,

            PixelFormat.TRANSLUCENT);
}

in This Code You added WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE remove this line its works and add touch event on view its works

For The Screen Overlay permission I am Referring to this post How to enable screen overlay permission by default

Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65
0

You can use my library just removing this method addWatermarkWithinApplicationLifecycle() which sticks the overlay with the app lifecycle (As you want to show the overlay all over the phone app).I tried to do it in a reusable way that you can hide and show overlay just a simple method call. Please also don't forget to add these permissions -

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

The gif preview of my library -

enter image description here

Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42