I am working on an application where I need to show the download speed in status bar. I have tried using
notificationBuilder.setSmallIcon()
but it takes a constant drawable as parameter. I want the number to change after every 3 seconds. I have seen many apps that show some number in status bar e.g temperature, download percentage etc. So, there is a way for sure. But I can't figure it out.
Asked
Active
Viewed 430 times
0

Hashir Sarwar
- 1,115
- 2
- 14
- 28
-
Check this guide https://medium.com/@britt.barak/notifications-part-2-progress-indicator-2aa4cdea24c6 – Ikazuchi Jun 28 '19 at 10:48
-
@Ikazuchi thats not what I want.. I want varying number to display as notification icon. I have added a screenshot in my question for further demonstration – Hashir Sarwar Jun 28 '19 at 10:52
-
please check this https://stackoverflow.com/questions/45376300/how-do-i-show-text-in-android-system-status-bar – AMAL K G Jun 28 '19 at 10:54
-
if it works please notify me – AMAL K G Jun 28 '19 at 10:54
-
@AMALKG i have already tried all these solutions. They are kind of outdated i guess. Nothing worked for me – Hashir Sarwar Jun 28 '19 at 10:59
1 Answers
0
You can achieve that by converting text string to bitmap drawable.
Use below method to convert your text to bitmap. And use return bitmap as smallIcon in notification builder.
public Bitmap textAsBitmap(String text, float textSize, int textColor) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(textSize);
paint.setColor(textColor);
paint.setTextAlign(Paint.Align.LEFT);
float baseline = -paint.ascent(); // ascent() is negative
int width = (int) (paint.measureText(text) + 0.5f); // round
int height = (int) (baseline + paint.descent() + 0.5f);
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawText(text, 0, baseline, paint);
return image;
}

DHAVAL A.
- 2,251
- 2
- 12
- 27
-
setSmallIcon doesn't accept `Bitmap` or `Icon`. notificationbuilder which had setSmallIcon(icon) is deprecated now – Hashir Sarwar Jun 28 '19 at 11:54