Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a notification Bar with Drawable
    text
    copied!<p>I am trying to create a custom Notification bar using a <code>Drawable</code> Object, but it don´t receive <code>Drawable</code> in the parameter, only <code>Bitmap</code>.</p> <h2>The Problem:</h2> <p>I need to get the icon of another application to set in my Notification. I tried use the code below:</p> <pre><code>getPackageManager().getApplicationIcon(packageName);//Return a Drawable Object //Problem Notification.Builder builder = new Notification.Builder(context); builder.setLargeIcon(Bitmap); //I don´t have this one, only a Drawable object. </code></pre> <p>As you can see. How I can use this Object to put in my Notification bar ?</p> <h2>Result expected:</h2> <p>Create a Notification Bar with the icon of another application, name and the Action to this one.</p> <p><b>SOLUTION:</b></p> <p>Thanks, pietmau and kadrei.</p> <p>Please use this code below:</p> <pre><code>public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width &gt; 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height &gt; 0 ? height : 1; Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } </code></pre> <p>and complete with this one:</p> <pre><code>Notification.Builder builder = new Notification.Builder(context); Drawable icon = getPackageManager().getApplicationIcon( packageName); Bitmap bitmapIcon = drawableToBitmap(icon); builder.setIcon(android.R.drawable.stat_sys_download_done).//It´s necessary put a resource here. If you don´t put any resource, then the Notification Bar is not show. setLargeIcon(bitmapIcon); Notification notification = builder.getNotification(); notificationManager.notify(yourId, notification); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload