Sohaib Raza
Sohaib Raza

Reputation: 41

Set the Drawable icon of other app to the notification builder

I am working on application, In that application I am getting other application info. And the info show on the notification builder. I am getting the other application icon, and want to show on the notification. But the getting info contain drawable image.

So, how can I show the draw able image on the notification. Here is my sample of code.

 String packageName=appSetting.getHeavyDrainingAppName();

 ApplicationInfo app = getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA);

 int iconId = ......????



 NotificationCompat.Builder mBuilder =
                            new NotificationCompat.Builder(context)
                                    .setSmallIcon(iconId)
                                    .setContentTitle(getResources().getString(R.string.heavy_drain_text))
                                    .setContentText("Tap to stop")
                                    .setOngoing(true);

Upvotes: 1

Views: 1800

Answers (1)

Lucas Ferraz
Lucas Ferraz

Reputation: 4152

There is some points about this question:

1st :

You can set the LargeIcon from a Drawable (instead of Resource id), like the following

Drawable icon = getActivity().getPackageManager().getApplicationIcon(packageName);

            Bitmap bitmap = ((BitmapDrawable)icon).getBitmap();

            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
                            .setLargeIcon(bitmap)
                            .setContentTitle("hahah")
                            .setContentText("Tap to stop")
                            .setOngoing(true);

2nd :

If you need to set a SmallIcon in API below 23, you will need to set a resource id like R.drawable.your_resource. The NotificationCompat.Builder does not allow you to use Drawables or Bitmaps insetSmallIcon()`.

3rd :

fortunately , the support has been expanded to Icon type on setSmallIcon() in version 23+, using the Notification.Builder, like following :

Drawable icon = getActivity().getPackageManager().getApplicationIcon(packageName);

            Bitmap bitmap = ((BitmapDrawable)icon).getBitmap();

            Notification.Builder mBuilder =
                    new Notification.Builder(context)
                            .setSmallIcon(Icon.createWithBitmap(bitmap))
                            .setLargeIcon(bitmap)
                            .setContentTitle("hahah")
                            .setContentText("Tap to stop")
                            .setOngoing(true);

Upvotes: 3

Related Questions