How to create an UI or a widget that sits on top of all applications in Android?
Asked Answered
K

2

8

Can I create an UI or a widget in Android that will sit on top of all applications? There are applications that have widgets like this. One example has a camera icon on top of all the applications that, when clicked, will capture the screen.

Knipe answered 16/7, 2012 at 5:3 Comment(0)
T
18

If you want to just display something, you can display it on top of everything even the lockscreen.

If you want something to be clickable, you can display it on top of anything except the lockscreen.

Here's a sample, modify to your needs:

Create a service and do the following:

//These three are our main components.
WindowManager wm;
LinearLayout ll;
WindowManager.LayoutParams ll_lp;

//Just a sample layout parameters.
ll_lp = new WindowManager.LayoutParams();
ll_lp.format = PixelFormat.TRANSLUCENT;
ll_lp.height = WindowManager.LayoutParams.FILL_PARENT;
ll_lp.width = WindowManager.LayoutParams.FILL_PARENT;
ll_lp.gravity = Gravity.CLIP_HORIZONTAL | Gravity.TOP;

//This one is necessary.
ll_lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

//Play around with these two.
ll_lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
ll_lp.flags = ll_lp.flags | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

//This is our main layout.
ll = new LinearLayout(this);
ll.setBackgroundColor(android.graphics.Color.argb(0, 0, 0, 0));
ll.setHapticFeedbackEnabled(true);

//And finally we add what we created to the screen.
wm.addView(ll, ll_lp);
Tyranny answered 16/7, 2012 at 5:37 Comment(4)
Thanks. It worked with android.permission.SYSTEM_ALERT_WINDOWKnipe
Hatcyl, can you please provide me with the entire code if you don't mind so that I will be able to understand the entire flow as well.Hewart
@hatcyl, have you tried the notifierpro app? It shows on top of lock screen as well. And it's clickable. What could that be?Mesenchyme
@Tyranny hey i run this code but got null pointer exception on wm object of window manager. can you tell me why it gives that..? i tried many other things but couldn't get it done.. :(Kaolack
E
3

The following are more options for how the view is displayed.

This will make it an overlay on top of everything (including lock screen), but not clickable. WindowManager.LayoutParams. TYPE_SYSTEM_OVERLAY

This will make it clickable, but it won't be over the lock screen WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

This will make it above everything (including lock screen) AND clickable. WindowManager.LayoutParams.TYPE_SYSTEM_ERROR

One thing to note about using "TYPE_SYSTEM_ERROR". If you wire-in a click event, anything it invokes will happen behind the lock screen.

Eisenhart answered 10/2, 2015 at 2:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.