Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to draw free on top of anything (any activity)

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?

like image 784
Allahjane Avatar asked May 15 '13 07:05

Allahjane


People also ask

What does it mean for an app to draw over another app?

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.

How do you programmatically grant the draw over other applications permission in android?

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.


2 Answers

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

like image 112
Fredszaq Avatar answered Nov 11 '22 11:11

Fredszaq


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.

like image 43
bogdan Avatar answered Nov 11 '22 10:11

bogdan