Icon with notification counter

Asked

Viewed 11,473 times

1

Does anyone know how I do a notifications counter on Android like Facebook, Whatsapp, follows the photo showing the phone icon. !http://imgur.com/Xar2jOv

  • As an icon I think it is not possible to have this dynamicity. But being a widget that shows a number positioned on top of the app logo is possible. In the widget you mount the layout as you like, take a look at: https://developer.android.com/guide/topics/appwidgets/index.html.

2 answers

2


Pure Android does not have this functionality, manufacturers add this functionality in their UI and is different in each of them to use this feature.

In the case of Samsung Touchwiz would be like this:

public static void setBadge(Context context, int count) {
    String launcherClassName = getLauncherClassName(context);
    if (launcherClassName == null) {
        return;
    }
    Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
    intent.putExtra("badge_count", count);
    intent.putExtra("badge_count_package_name", context.getPackageName());
    intent.putExtra("badge_count_class_name", launcherClassName);
    context.sendBroadcast(intent);
}

public static String getLauncherClassName(Context context) {

    PackageManager pm = context.getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
    for (ResolveInfo resolveInfo : resolveInfos) {
        String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
        if (pkgName.equalsIgnoreCase(context.getPackageName())) {
            String className = resolveInfo.activityInfo.name;
            return className;
        }
    }
    return null;
}

This I’ve tested and know it works on a Samsung Galaxy Tab 2, you can check it on stackoverflow topic where I got the code, there is also an example for Sony devices, but I never got to test.

1

There’s this library that does just that here. Android does not allow you to change the icon after APK is generated, basically the library View Badger create a widget above the app icon.

Remember that the creation of counters over the icon is considered as bad practice, according to the documentation of design android. Notifications are displayed in the notification bar.

  • I’ve used this library, very simple it’s worth taking a look

Browser other questions tagged

You are not signed in. Login or sign up in order to post.