yedidyak
yedidyak

Reputation: 1984

Adding text to a notification icon

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

Upvotes: 6

Views: 903

Answers (2)

Kottur
Kottur

Reputation: 11

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

Upvotes: 0

aragaer
aragaer

Reputation: 17858

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.

Upvotes: 1

Related Questions