How do you draw a view on top of all other activities regardless of what they are showing and without using transparent activities or consuming its touch events.
Its like to display a custom icon on the screen on top of all other apps that reacts when you touch it but you can still touch the other views on the screen.
Example: facebook chat heads that displays a dragable and clickable icon on screen regardless of what you are doing whether you are on home screen or app menus or any app. Still you can click the chat head icon and background app elements seperately
How to do anything like that?
Using an Android feature called "Draw over other apps," in which an image or dialog box appears on top of anything else that might be on your device's screen. The "chat heads" used by Facebook Messenger are one example of how this works. Google routinely grants apps the right to draw over other apps if they request it.
In order to draw over other apps we require, android. permission. SYSTEM_ALERT_WINDOW permissions and for android with API version > 23 we need to ask for this on runtime.
Take a look at this cool article, I think that's exactly what you want :
http://www.piwai.info/chatheads-basics
In short : you want to add this permission to your manifest :
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
For api >= 23, you'll need to request the runtime permission Settings.ACTION_MANAGE_OVERLAY_PERMISSION
Then in a service, get the window manager
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
and add your views with the addView
method
Take a look at WindowManager
. You can do something like (WindowManager)mContext.getSystemService("window");
if you want to get the root view and use the method addView(view, params)
if you want to add a view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With