Android 11 Bubble API is finally here!

Tram Ho

Remember about 2015 2016 something I often heard requests to make a “ball” like Facebook Messenger. Yes, that’s right, surely those who use Facebook Messenger on Android will be very familiar with a circular icon floating on the screen when receiving the message as shown below.

A lot of ideas have been raised on how to do this, of which the most common idea is to create a floating window on top of other apps through the SYSTEM_ALERT_WINDOW request. However, this is a discouraged idea, according to the Android documentation

Very few apps should use this permission; these windows are intended for system-level interaction with the user. This permission is inherently used at the system level to interact with the user. For a while, until recently, Google finally brought this cool feature to Android called Bubble. Starting with Android 10, developers have been able to experiment with this feature by enabling Settings ▸ Developer Options ▸ Bubbles. And on Android 11, it’s officially in use. Now let’s try to deploy. First of all, you can read more about Bubble here https://developer.android.com/guide/topics/ui/bubbles First you need to set targetSdkVersion to 30 (Android 11 ) in build.gradle Next, you need to build an activity so that when the user clicks on Bubble, it will expand.

Note the last two lines, as in the documentation, The activity must be resizeable and embedded

Next step, please pay attention to the following line

Bubbles are built into the Notification system. They float on top of other app content and follow the user wherever they go. Bubbles can be expanded to reveal app functionality and information, and can be collapsed when not being used. Thus, Bubble is built with Notification system, which means that for Bubble to “jump”, a notification is required to fire. So in this step, we will build a notification, starting from creating a channel

However, not all notifications can show Bubble, but need to be the notification type conversation, you can read more here https://developer.android.com/guide/topics/ui/conversations#api-notifications Now We will build a shortcut for our notification (note, this is just a demo code so id, name, icon … are approximate and use hard-code, you should not use real code)

Next, we create Bubble Intent and metadata to add to notification

Finally, create notification:

At this point, you can try notifcation shot

However, you will not see Bubble appear? Why? Did I follow the document correctly? That is because the code in the document does not satisfy the condition

but need to be the notification type conversation In the build notification section above, add a few lines of code to become as follows

That’s it, now let’s try the new feature compared to Facebook’s. Happy coding !!!

Share the news now

Source : Viblo