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?
Asked
Active
Viewed 2,350 times
3 Answers
0
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);

Ruslan
- 87
- 1
- 10
0
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.

Frusty
- 11
- 1
-1
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>);

Hyperion
- 382
- 8
- 16
-
How do you change the icon later on though? Specifically I want to change the action button's icon once it is pressed – user1840378 Jul 08 '15 at 20:00