229

How does Facebook create the Chat Heads on Android? What is the API to create the floating views on top of all other views?

Ryan M
  • 11,512
  • 21
  • 38
  • 50
Daniel A. White
  • 174,715
  • 42
  • 343
  • 413

3 Answers3

218

This one:

Allows an application to open windows using the type TYPE_SYSTEM_ALERT, shown on top of all other applications. Very few applications should use this permission; these windows are intended for system-level interaction with the user.

Constant Value: "android.permission.SYSTEM_ALERT_WINDOW"

//EDIT: The full code here:

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

Don't forget to start the service somehow:

startService(new Intent(context, ChatHeadService.class));

.. And add this service to your Manifest.

rekire
  • 45,039
  • 29
  • 149
  • 249
Waza_Be
  • 39,545
  • 47
  • 176
  • 256
  • 4
    There's more weird stuff, I think. What about handling input outside the "head" as well as dragability? I think you'd at least need [FLAG_NOT_TOUCH_MODAL](http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_NOT_TOUCH_MODAL), as well as some clever logic to update the Window attributes (i.e., move it) while you're dragging it. – Delyan Apr 12 '13 at 21:29
  • 2
    in that how to remove chat -heads ? – Nirav Mehta Aug 12 '14 at 07:10
  • 6
    Thought that it's worth mentioning an SDK I developed for creating floating UI: www.tooleap.com – Arik Sep 11 '14 at 21:06
  • @NiravMehta Are you able to remove the chat heads ? – KK_07k11A0585 Jan 02 '15 at 07:20
  • how to remove the margin of chat head.Because in the above code output the circle have some space from the border.In Facebook the bubble doesn't have that space – Debugger Feb 10 '15 at 12:30
  • @HenryChuang how to use this library in my app? –  Oct 23 '16 at 13:05
  • @14bce109 copy ChatHeadService.java to your project, if your targetSDK bigger than 23, you should implement runtimepermission(somebody say that app published to Google Play does not need to request permission "android.permission.SYSTEM_ALERT_WINDOW", you can try it) – HenryChuang Oct 23 '16 at 15:58
  • How to keep the Cheat Head when Application is closed/terminated ? – Salman Khakwani Nov 28 '16 at 12:21
  • @HenryChuang hey i have see your reference code is very good but when we click your chat head it will open MyDilog.java class now i just want when this activity is open chat head should not overlay in that activity (Mydialog.java) i mens like fb icon should not overlay on edit text can you help? – Jayman Jani Dec 22 '16 at 12:38
52

As a rule, Android activities are full screen, conceptually dedicated UIs that take all the interaction. There are a few exceptions to this. For a start, there are popup dialogs that don't fill the screen. Another is the Android toast, which is a non-interactive popup - you can't touch it, and if you try it'll go to whatever's underneath.

You can do your own special UIs too. You can add views directly to the WindowManager, specifying a type flag. Chat Heads probably uses TYPE_PHONE. There are a few similar types, but the purpose is the same: special purpose overlays that can appear over the top of anything else without the parent application apparently being present.

That only gets you so far, though, because of problems with interaction. At first, your overlay will absorb all the interaction, so not only does the head get events, but you block interaction to everything underneath.

You configure this behaviour using the LayoutParams. FLAG_NOT_TOUCH_MODAL means that events outside of your display area go to the underlying UIs. You'll now find it works, but that other bad things still happen, like the back/menu buttons don't get directed to apps, plus no keyboard. To solve that you need FLAG_NOT_FOCUSABLE.

You get a side effect from the non-focusable bit too, which is no nice interactions with your overlay any more, e.g. button presses. You can get some basic touch events though, which you can always do maths on, and that's probably enough for Chat Heads. Just be aware that it leaves you on your own in plenty of areas, like UI animation.

A good overview of the detail, including allowing for selective interaction consumption, can be found in this StackOverflow thread. In particular one of the answer links will eventually take you here, which is a good example project. Note that ICS changed how this works a little, but the threads explain that.

This is all public API stuff, but it doesn't really seem like a mainstream thing that one ought to be doing as a matter of course. The documentation is littered with references to special system app behaviours, and with good reason; what if everyone did it?

Community
  • 1
  • 1
Rob Pridham
  • 4,086
  • 1
  • 22
  • 32
  • Could I block somehow orientation changes for these view? If underlying activity switches orientation, my view also changes his orientation. – M G Jul 04 '13 at 23:23
  • 1
    No. The orientation change is device-wide, not just for the activity. – Rob Pridham Jul 05 '13 at 08:08
7

Springy heads gives spring based behaviour of chat heads out of the box. All you have to define is the drawable for the chat head and the fragment to open once the chat head is clicked. The chat heads collapse when minimized and follow the finger when dragged.

The project includes a demo app which demonstrates all the built in functionality. To use it, you need to add this into your gradle dependencies.

compile 'com.flipkart.springyheads:library:0.9.6'
Rando Hinn
  • 1,186
  • 18
  • 33
Kiran Kumar
  • 1,182
  • 8
  • 10
  • Hey @KiranKumar , you have created a very clear library.. I just love it.. I am trying to learn various aspects from this library.. I have been trying to run it outside the application window, somehow i am slightly successful in doing that so.. But, the problem arises when i click the chat head outside the window of the app.. the fragment wouldn't be able to open and returning java.lang.OutOfMemoryError.. if it could be possible from your side to give me some way through this.. It will be gladly honored.. – arraystack Mar 09 '16 at 14:50
  • @arraystack now you can run it in a service. Check the list if branches in the GitHub repo – Kiran Kumar Nov 09 '16 at 17:20
  • @KiranKumar Thank you, The problem with new lib is the permission of Draw over application in Marshmallow Version – arraystack Dec 16 '16 at 09:51