6

I want to make a notification icon with text over it, to show for example the percent battery remaining. Is there a way to do that that doesn't involve 100+ separate icons?

I've looked all over but couldn't find a way.

Thanks

0

2 Answers 2

1

100+ separate icons unfortunately. Because of security reasons android only accepts resource IDs that also must not be custom resource type.

Would be happy to know if I'm wrong though.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok then, next question: Is there any way of quickly generating 100 similar icons?
That's where you can use android canvases and save bitmaps to file. In a separate application of course.
No news for this for the modern age (2022)?
0

You can draw a bitmap using this function

 fun createTemperatureBitmap(context: Context, temperature: String): Bitmap { val size = 48 val scale = context.resources.displayMetrics.density val bitmap = Bitmap.createBitmap((size * scale).toInt(), (size * scale).toInt(), Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.WHITE textSize = 24 * scale textAlign = Paint.Align.CENTER } val x = bitmap.width / 2f val y = bitmap.height / 2f - (paint.descent() + paint.ascent()) / 2 canvas.drawText(temperature, x, y, paint) return bitmap } 

To add it as an icon, you must use

val iconBitmap = createTemperatureBitmap(context, temperature) 

and

.setSmallIcon(IconCompat.createWithBitmap(iconBitmap)) 

*Only for api M and higher

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.