Skip to content Skip to sidebar Skip to footer

How Do I Make My Full-screen Overlay Remain Full-screen After Orientation Changes?

I am making an app that creates tiny sprite animations that walk around your screen. I have a main activity, with a button 'start service'. This starts a service, which (in onCreat

Solution 1:

I was misled by the answer to a previous question "How to create always-top fullscreen overlay activity in Android". Maybe it worked for an older API level? I'm using API level 24.

That particular answer had recommended:

final WindowManager.LayoutParamsparams=newWindowManager.LayoutParams(
    WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
    PixelFormat.TRANSLUCENT
);

There is a problem with this. The constructors that exist for WindowManager.LayoutParams are as follows:

enter image description here

So the WindowManager.LayoutParams.FLAG_FULLSCREEN flag gets used as an explicit value for int w and int h. This is no good!


I found that the correct construction for a full-screen overlay is like so:

final WindowManager.LayoutParamsparams=newWindowManager.LayoutParams(
    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, // TYPE_SYSTEM_ALERT is denied in apiLevel >=19
    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_FULLSCREEN,
    PixelFormat.TRANSLUCENT
);

This means we no longer explicitly specify a width and height. The layout relies entirely on our flags instead.

Yes, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN is a required flag still; it is necessary if you want to draw over decorations such as the status bar.

WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY should be used instead of TYPE_SYSTEM_ALERT in API level >=19.


Bonus notes (if you're reading this, you're probably trying to make a full-screen overlay):

Your manifest will need the following permissions (explanation here):

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

My understanding is that SYSTEM_ALERT_WINDOW is the actual permission required, but that ACTION_MANAGE_OVERLAY_PERMISSION is needed also: it lets you request at runtime that the user grant the SYSTEM_ALERT_WINDOW privilege.

Solution 2:

I think your situation can be solved with an alternative approach.

  1. Create a Full Screen custom view (you can set the appropriate flag in the constructor of the custom view)
  2. Override onSizeChanged() in the custom view. This will give you the Canvas height and width. This is optional. If you want to fill the whole custom view with a translucent color, this won't be necessary.
  3. Use the width and height from above, or simply use canvas.drawColor() in the onDraw() of the custom view.

This will ensure that whenever the view is recreated, it will be redrawn to fill the whole screen(canvas).

Post a Comment for "How Do I Make My Full-screen Overlay Remain Full-screen After Orientation Changes?"