So, the very first question is: How can we do that? An activity with transparent background? Actually, there’s no activityhere. Instead, we’ll user service!
As you know, activityand dialoghave their own Window instance which provides standard UI policies such as a background, title area, default key processing, etc.
Even the service has Window! So, the solution here is that we will use the service’s Window to draw our desired view (that can be overlay on other application!).
In order to do that, you have to follow below steps:
1 - Add permission
Open AndroidManifest.xml file and add the below permission:
Regards to Android documents:
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.
2 - Create Floating View Service
Create a class called FloatingViewService that extends Service, contains Window instance and a FrameLayout object used to display your view:
Inside onCreate method, we just need to set up the view with the corresponding layout params and finally call WindowManager.addView():
Please note that 3 attributes below are mandatory for floating view:
Don’t forget to declare FloatingViewService in the AndroidManifest.xml file:
Finally, start the service to trigger your floating view:
 3 - Demo Floating View like Facebook Chatheads
The first two sections showed you how to implement floating view like Facebook Chatheads. In this section, I’ll make a very simple application to demonstrate it.
The view in this application will be an ImageView that you can drag around.
Firstly, create an activity with a Button that allows you to turn on/off the floating view:
Then, create FloatingViewService:
To be able to drag the “chatheads” around, you need to handle the OnTouchListener of the ImageView:
In the MainActivity, we handle the Button clicked event to show or hide floating view:
Finally, run the demo application and enjoy the result:
4 - Source Code Floating View like Facebook Chatheads