Reputation: 1260
I want the Notification
icon to be a number that can be anywhere from 1 to 1000 depending on conditions.
Is there a way to do this dynamically (e.g. generate an Icon
from a String
and use it in setSmallIcon()
) without manually creating all these numbers as an image file and calling them dynamically?
Upvotes: 0
Views: 263
Reputation: 19427
Unfortunately there is no way (i'm aware of) to do this below API level 23.
On API level 23+:
You could use Canvas.drawText()
to create a Bitmap
from your String
.
For example:
public Bitmap createBitmapFromString(String string) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(50); // size is in pixels
Rect textBounds = new Rect();
paint.getTextBounds(string, 0, string.length(), textBounds);
Bitmap bitmap = Bitmap.createBitmap(textBounds.width(), textBounds.height(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(string, -textBounds.left,
textBounds.height() - textBounds.bottom, paint);
return bitmap;
}
After that you could create an Icon
using this Bitmap
with Icon.createWithBitmap()
.
(this method was added in API level 23)
And then pass this Icon
to setSmallIcon()
.
(setSmallIcon(Icon icon)
was also added in API level 23)
Upvotes: 1