In this tutorial, we will explore how to get the unread count bubble on the app icon on Android 8.0 Oreo update. This feature allows users to see at a glance how many unread messages, notifications, or updates they have without opening the app itself.
Prerequisites
To follow along with this tutorial, you will need:
- An Android device running on Android 8.0 Oreo or later
- A basic understanding of Android development and XML layout files
Step 1: Add Badge Support Library to your Project
To use the unread count bubble feature, we need to add the Badge Support Library to our project. This library provides a simple way to display badges on app icons.
Open your project’s build.gradle file and add the following dependency:
implementation 'me.leolin:ShortcutBadger:1.1.22@aar'
Step 2: Create a Broadcast Receiver Class
Create a new Java class called BadgeReceiver.java. This class will handle receiving broadcast messages and updating the badge count.
public class BadgeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Get the number of unread messages from your data source
int count = getUnreadCount();
// Update the badge count
ShortcutBadger.applyCount(context, count);
}
private int getUnreadCount() {
// Implement your logic to get the unread count
// from your data source (e.g., database or API)
}
}
Step 3: Register the Broadcast Receiver
In your AndroidManifest.xml file, add the following code to register the BadgeReceiver class as a broadcast receiver:
<receiver Android:name=".BadgeReceiver">
<intent-filter>
<action Android:name="Android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Step 4: Update Your App Icon Badge Count
In your app’s main activity (or any other suitable activity), add the following code to update the badge count:
// Get the number of unread messages from your data source
int count = getUnreadCount();
// Update the badge count
ShortcutBadger.applyCount(getApplicationContext(), count);
Make sure to replace getUnreadCount() with your implementation to retrieve the actual unread count from your data source.
Note:
The badge count will only be displayed on devices that support this feature. If a device does not support it, the badge count will be ignored.
Conclusion
Congratulations! You have successfully implemented the unread count bubble on the app icon for Android 8.0 Oreo and later. Now, users can easily see how many unread messages or notifications they have without opening the app.
This feature can greatly improve user experience by providing quick access to important information, so make sure to utilize it wisely in your app.