How to create always-top fullscreen overlay activity in Android
Asked Answered
W

3

22

I'd like to be able to create an Activity that is always at the front of display of Android. It should receive no input, just pass it down to whatever application is next below it. Something like a HUD.

I was able to research that I need to set underlying window type to TYPE_SYSTEM_ALERT but it looks like Android is ignoring my code - no exception thrown even if I delete android.permission.SYSTEM_ALERT_WINDOW permission from manifest. (it is required to use this window type). When I tried to use ALERT type on dialog, it worked OK, but I cannot make dialog into full screen transparent entity. Here is my code, maybe there is something simple missing.

public void onCreate(Bundle savedInstanceState) {       
     super.onCreate(savedInstanceState);        

     requestWindowFeature(Window.FEATURE_NO_TITLE);
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
     getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);             
     getWindow().setBackgroundDrawableResource(android.R.color.transparent);
     getWindow().setFormat(PixelFormat.TRANSLUCENT);
     setContentView(R.layout.main);
}

Translucent setting has to be enabled externally in xml manifest, otherwise it also didn't work.

 <item name="android:windowIsTranslucent">true</item>
Wisteria answered 10/4, 2012 at 19:43 Comment(1)
what about for oreoImponderable
W
26
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);

WindowManager wm = (WindowManager) getApplicationContext()
    .getSystemService(Context.WINDOW_SERVICE);

ViewGroup mTopView = (ViewGroup) App.inflater.inflate(R.layout.main, null);
getWindow().setAttributes(params);
wm.addView(mTopView, params);
Wisteria answered 11/4, 2012 at 14:3 Comment(6)
What did your onCreate method end up looking like? I'm trying to test this myself now.Godard
ViewGroup mTopView = (ViewGroup) App.inflater.inflate(R.layout.main, null); @Wisteria what is APP here ?Celik
@Wisteria what is R.layout.main? is it the activity layout? or a filler view transparent?Suber
@ rana use getLayoutInflater() insteadFarrica
Note that this is a window rather than an activity.Revet
How to use finish() here. It is not closing with finish() methodHelton
I
5

I found that the accepted answer (by tmouse) did not quite work for me. Maybe it worked for an older API level? I'm using API level 24.

That particular answer had recommended:

final WindowManager.LayoutParams params = new WindowManager.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! Your view's dimensions get set to 1024*1024 — so it does not fill the screen. And even if you explicitly set the layout's width and height to match the device's dimensions: they will not update when the screen orientation changes. We need a different approach…


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

final WindowManager.LayoutParams params = new WindowManager.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. This is a note I wrote a while ago, sadly I've been unable to find citation to corroborate why I thought that, so take that with a pinch of salt.


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

Your manifest will need these permissions (explanation here):

<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION"/>
<uses-permission android: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.

I provide here the source code to my working API level 24 app that creates a full-screen overlay.

Inflationary answered 4/1, 2017 at 23:44 Comment(0)
D
-1

Check out this

mention it in your AndroidManifest.xml file

<activity android:name=".MyActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />

if you want to make your Activity full screen and transparent also

check out this

<activity android:name=".MyActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
Drive answered 11/4, 2012 at 9:38 Comment(3)
Thanks, but what above achieves is only setting up activity as fulscreen/transparent. What I'm trying to do is to make activity ALWAYS on top, regardless of what user is doing (launching other applications, locking phone, etc).Wisteria
did you used <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"></uses-permission> in Manifest file if not check out thisDrive
Yes, as I've written I've used this but as above code is executed this permission is never used (which is wrong - permission is required for such a window to appear).Wisteria

© 2022 - 2024 — McMap. All rights reserved.