46

I want to implement notifications like the one in the following image.

Notification appears any time. I think it's of course a background service waiting for new messages from the server then shows this. What I think this is an activity implemented as dialog with this custom UI. Am I correct? And is it a normal startActivity method from the service? And how do I do the transition animation to make it appears slowly from left to right with zooming when show up?

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MSaudi
  • 4,442
  • 2
  • 40
  • 65
  • https://github.com/henrychuangtw/Android-ChatHead – HenryChuang Sep 20 '16 at 08:32
  • 1
    Possible duplicate of [What APIs in Android is Facebook using to create Chat Heads?](http://stackoverflow.com/questions/15975988/what-apis-in-android-is-facebook-using-to-create-chat-heads) – Justin Mitchell Nov 24 '16 at 05:25

1 Answers1

52

Check out this link http://www.piwai.info/chatheads-basics. He provides information about how to add them on your screen.

The trick is to add a View to the WindowManager like following code

private WindowManager windowManager;
private ImageView chatHead;

public void addView()
{
  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);
}

Don't forget to add the permission <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Apoorv
  • 13,470
  • 4
  • 27
  • 33
  • 5
    can you tell me how can i add a layout (near the circle) just as facebook does when we click on the circular notification. – cafebabe1991 Dec 24 '13 at 20:10
  • You should have provided credit to the original answer on this subject, where you copied the exact same code from. – Justin Mitchell Nov 24 '16 at 02:54
  • 2
    @JustinMitchell We have the same code because we used the same source. I did not copy his answer. – Apoorv Nov 24 '16 at 03:36
  • @Amnesh Goel I noticed that you didn't accept my edited changes, yet added your own. It has several spelling mistakes, could you please revert to the changes that I added. – Justin Mitchell Nov 24 '16 at 05:22