2
votes

When you build a notification and add an action button using .addAction(int icon, CharSequence title, PendingIntent intent) is there a way to change the icon when pressed?

3

3 Answers

0
votes

To change the icon of action at realtime, you need to set new action with same title and new icon, and then restart notification. For example:

notificationBuilder.actions[1] = new Notification.Action(R.drawable.pause, "play", pendingIntent);
startForeground(101, notificationBuilder);

or

notificationBuilder.actions[1] = new Notification.Action(R.drawable.pause, "play", pendingIntent);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(101, notificationBuilder);
0
votes

Use the broadcast, process the button event in it (to change the icon, create a new notification with the changed icon and call it with the previous id), and pass the necessary parameters to the intent.

-1
votes

You need to create a drawable like below, and give it to addAction

button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/actionbarcontacts_on" android:state_pressed="true"/>
  <item android:drawable="@drawable/actionbarcontacts_on" android:state_focused="true"/>
  <item android:drawable="@drawable/actionbarcontacts_off"/>
</selector>

addAction(R.drawable.button, <title>, <intent>);