NullPointerException
NullPointerException

Reputation: 37731

It is possible to change the Icon of the status bar (notification icon) dynamically?

I have an android APP, with a lot of activities.

In the login activiti of my app, i start a notification icon in the status bar, and it is fixed there until my app stops. Ok, it works.

But now i need one more thing, i need to changue the icon dynamically, programatically, with a service of my app. How can i do it?

How can i access to the notification icon of my app and then change the icon?

I would appreciate code examples to illustrate how to achieve this.

Upvotes: 5

Views: 8956

Answers (2)

user511564
user511564

Reputation: 376

You could use the iconLevel on the Notification: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#More

Create a xml file in res/drawable/myicon.xml with different level (different icon) http://developer.android.com/reference/android/graphics/drawable/LevelListDrawable.html

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:maxLevel="0" android:drawable="@drawable/ic_wifi_signal_1" />
  <item android:maxLevel="1" android:drawable="@drawable/ic_wifi_signal_2" />
  <item android:maxLevel="2" android:drawable="@drawable/ic_wifi_signal_3" />
</level-list>

and set or (update) the level with:

Notification mNotification = new Notification(icon, tickerText, when);
mNotification.iconLevel = 1;
mNoticationManager.notify(NOTIFICATION_ID, mNotification);

Upvotes: 8

CommonsWare
CommonsWare

Reputation: 1007584

Just call notify() again on NotificationManager with a new Notification but the same unique ID as you used for the first one. It will replace your icon of the existing Notification (or display the new Notification if the user cleared the first one).

Upvotes: 8

Related Questions