Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a popup overlay that can be displayed over any other app in Android

How can I implement a popup overlay for an app that can be displayed over any other app.

Facebook implemented a very similar feature called Chatheads in its new Facebook Messanger version. I was really surprised to see that this is some how possible. On the image, you see a chathead (dog on the right) over another app.

Facebook Messanger's Chathead over other app

like image 698
Flo Avatar asked Apr 13 '13 16:04

Flo


People also ask

How do I create a pop up screen on my Android?

Use setWidth(int) and setHeight(int) . Set the layout type for this window. Display the content view in a popup window anchored to the bottom-left corner of the anchor view. Displays the content view in a popup window anchored to the corner of another view.

How do you put a drawing over another app?

On Android version 6.0 (Marshmallow) or later, "Drawing over other apps" permission is required to use the AirCircle. Go to Mobizen > Settings > enable "Drawing over other apps" option. Enable "Drawing over other apps".

What is display over other apps in Android?

A screen overlay is a part of an app that displays over other apps, such as the chat heads in Facebook Messenger. If you receive a screen overlay error because an app is blocking part of another app on your screen, you can turn off this permission.


2 Answers

Full source code is here: http://www.piwai.info/chatheads-basics
Note: You will need <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

public class ChatHeadService extends Service {    private WindowManager windowManager;   private ImageView chatHead;    @Override public IBinder onBind(Intent intent) {     // Not used     return null;   }    @Override public void onCreate() {     super.onCreate();      windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);      chatHead = new ImageView(this);     chatHead.setImageResource(R.drawable.android_head);      WindowManager.LayoutParams params = new WindowManager.LayoutParams(         WindowManager.LayoutParams.WRAP_CONTENT,         WindowManager.LayoutParams.WRAP_CONTENT,         WindowManager.LayoutParams.TYPE_PHONE,         WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,         PixelFormat.TRANSLUCENT);      params.gravity = Gravity.TOP | Gravity.LEFT;     params.x = 0;     params.y = 100;      windowManager.addView(chatHead, params);   }    @Override   public void onDestroy() {     super.onDestroy();     if (chatHead != null) windowManager.removeView(chatHead);   } } 
like image 125
Ankita Bansal Avatar answered Sep 18 '22 00:09

Ankita Bansal


This is a minimal, simple, and general example of a floating "chathead"-style overlay.

It uses the following code to make things float:

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); chatHead = new ImageView(this); chatHead.setImageResource(R.drawable.floating); final WindowManager.LayoutParams params = new WindowManager.LayoutParams(     WindowManager.LayoutParams.WRAP_CONTENT,     WindowManager.LayoutParams.WRAP_CONTENT,     WindowManager.LayoutParams.TYPE_PHONE,     WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,     PixelFormat.TRANSLUCENT); 

The full source source can be found here (under the Apache license).

like image 40
ahe Avatar answered Sep 21 '22 00:09

ahe