36

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

Piyush
  • 1,744
  • 1
  • 15
  • 28
Flo
  • 27,355
  • 15
  • 87
  • 125
  • I think this is a legitimate question. It might be poorly stated for some people. Rephrase the question so that it focuses more on the screen popup overlays on any app rather than Facebook Messenger itself, attribute it as your example. Screenshot also is nice to give context. – Jeremy Edwards Apr 13 '13 at 22:19
  • I hope my question is better now. – Flo Apr 14 '13 at 10:13
  • Have a look at the Facebook Messanger comments on Google Play. For some people it doesn't work, it may have something to do with all those TouchWizzes and HTC Senses... – Michał Klimczak Apr 14 '13 at 10:20
  • No no, for me it's working I want to know how I can implement such a behavior for my own app. – Flo Apr 14 '13 at 10:43
  • I don't understand why this question gets closed. – Flo Apr 14 '13 at 10:44
  • 4
    As I said before I believe this question is legitimate. Any of the users that closed this question should really explain why it was closed. There is no communication here. – Jeremy Edwards Apr 16 '13 at 04:54
  • 5
    http://www.piwai.info/chatheads-basics/ is the answer. – Jeremy Edwards Apr 16 '13 at 04:56
  • hello @Jeremy Edwards your link example helpful to me but how i close chaehead ? – Nirav Mehta Aug 12 '14 at 07:03
  • Your Question has been answered check this link out . http://stackoverflow.com/questions/15975988/what-apis-in-android-is-facebook-using-to-create-chat-heads – Ankita Bansal Jun 20 '15 at 06:52
  • https://github.com/henrychuangtw/Android-ChatHead – HenryChuang Sep 20 '16 at 08:32

5 Answers5

7

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);
  }
}
Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
Ankita Bansal
  • 335
  • 1
  • 5
  • 11
4

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).

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
ahe
  • 227
  • 1
  • 7
  • TYPE_PHONE is now deprecated and throws `WindowManager$BadTokenException ... permission denied for window type 2002` on api 28> – John Sardinha Mar 19 '19 at 23:28
2

i know its late to post the answer, i will post it anyway for other people who are seeking the answer

AndroidFloatingImage

Kosh
  • 6,140
  • 3
  • 36
  • 67
2

There's a library for that if you want Messenger like behavior: Bubbles.

If you prefer implement it yourself look at Window Manager as others suggest. You can also take a look at the source code of Bubbles to get inspired.

Louis CAD
  • 10,965
  • 2
  • 39
  • 58
0

Every Activity, dialog as well as service is attached with a window. Facebook keeps a service running in background and in the service they fetch window manager object by

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

Create Layout Params needed while adding your view

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);

now add your view to window manager by following method

windowManager.addView(yourView, params);
Neeraj Kumar
  • 943
  • 4
  • 16
  • TYPE_PHONE is now deprecated and throws `WindowManager$BadTokenException ... permission denied for window type 2002` on api 28> – John Sardinha Mar 19 '19 at 23:28