简体   繁体   中英

How to dynamically change notification icon color?

I use several hundred drawables within my app to dynamically display data with a notification icon in the status bar. Each is a PNG consisting of white text and a transparent background. I would like to change the white to another color in certain situations, but it seems like there must be a better way to do so other than re-creating hundreds of identical icons in different colors. Is it possible to do this with code?

I have found methods to change the color of a Drawable (ie: How to change colors of a Drawable in Android? ), but cannot figure out how to apply this to a notification icon. NotifBuilder.setSmallIcon() calls for an int , not a Drawable .

I think it could work with a LevelListDrawable combined with a LayerDrawable :

LevelListDrawable : A resource that manages a number of alternate Drawables, each assigned a maximum numerical value. Setting the level value of the object with setLevel(int) will load the image with the next greater or equal value assigned to its max attribute. A good example use of a LevelListDrawable would be a battery level indicator icon, with different images to indicate the current battery level.

LayerDrawable : A Drawable that manages an array of other Drawables. These are drawn in array order, so the element with the largest index will be drawn on top.

Try this:

  • a LevelListDrawable XML that contains as many levels as colors (eg level 1 = red, level 2 = blue etc)
  • each level references a LayerDrawable that contains 2 items, the background color 1st, the PNG icon last
  • repeat for all your icons
  • when you build the Notification, set the iconLevel property on the Notification object to the value that corresponds to the color you want

You'd still have many XML drawables, but that's more dynamic than a separate PNG for each icon/color combination.

Colors should NOT be used for notification icons, that are expected not to have colors. [1]

Colored icons may work on some older Android versions, but will not work on newer platforms. [2]

[1] http://developer.android.com/design/style/iconography.html#notification

[2] https://developer.android.com/preview/notifications.html#guidelines , see the DO and DON'T under "Use distinct icons" section

Try this

  mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // Sets an ID for the notification, so it can be updated
    int notifyID = 1;
    mNotifyBuilder = new NotificationCompat.Builder(this)
    .setContentTitle("New Message")
    .setContentText("You've received new messages.")
    .setSmallIcon(R.drawable.ic_notify_status)
    numMessages = 0;
    // Start of a loop that processes data and then notifies the user
    ...
    mNotifyBuilder.setContentText(currentText)
    .setNumber(++numMessages);
    // Because the ID remains unchanged, the existing notification is
    // updated.
    mNotificationManager.notify(
    notifyID,
    mNotifyBuilder.build());
    ...

See http://developer.android.com/training/notify-user/managing.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM