1

I am making drawable resource using following reference https://developer.android.com/develop/ui/views/graphics/drawables#java

MyDrawable mydrawing = new MyDrawable(myText);
ImageView image = findViewById(R.id.imageView);
image.setImageDrawable(mydrawing);

Above example shows how to make drawable image by program and assign to setimageDrawable(). But I need to get resource id of this drawable object to assign into Notification Bar build.setSmallIcon(xx) which requires int type resource Id. How to get resource Id from this drawable object?

MyDrawable mydrawing = new MyDrawable(myText);
mBuilder = new NotificationCompat.Builder(context,"default")
        .setContent(mRemoteViews)
        .setOnlyAlertOnce(true)
        .setAutoCancel(false)
        .setOngoing(true)
        .setSmallIcon(mydrawing); // <-- Error because mydrawing is not int type

MyDrawable.java makes drawable with given character

1 Answers1

0

You should convert your custom drawable into a bitmap using one of the methods described here. Then create an IconCompat object and provide that object to setSmallIcon:

MyDrawable mydrawing = new MyDrawable(myText);
Bitmap myBitmap = drawableToBitmap(mydrawing);
IconCompat smallIcon = IconCompat.createWithBitmap(myBitmap);

mBuilder = new NotificationCompat.Builder(context,"default")
        .setContent(mRemoteViews)
        .setOnlyAlertOnce(true)
        .setAutoCancel(false)
        .setOngoing(true)
        .setSmallIcon(smallIcon);
Lino
  • 5,084
  • 3
  • 21
  • 39