1

I'm trying to implement media player controls into my notification. I need the play/pause button to change between the "play" drawable and the "pause" drawable dynamically (basically on the user's touch). For example, as soon as the user touches the "pause" button, it needs to change to "play. When the user touches the "play" button, it needs to change back to "pause".

I'm assuming the best way to do this is by creating a StateListDrawable XML and setting it as the action button's drawable. Unfortunately, I can't seem to get the StateListDrawable to work. Here's my XML file for the drawable:

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android" >  
    <item android:state_first="true"  
          android:drawable="@drawable/pause_track_notification" />  
    <item android:state_last="true"
          android:drawable="@drawable/play_track_notification" />  
</selector> 

Am I missing anything important in the XML? Right now, I'm just getting an empty space where the play/pause button is supposed to show up. How do I get this to work? Thanks!

NewGradDev
  • 1,004
  • 6
  • 16
  • 35

1 Answers1

1

Use this for a Toggle button and the following selector:

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android" >  
    <item android:state_checked="true" 
          android:drawable="@drawable/pause_track_notification" />  
    <item android:state_checked="false"
          android:drawable="@drawable/play_track_notification" />
    <item android:drawable="@drawable/play_track_notification" /> <!-- the default -->
</selector>
g00dy
  • 6,752
  • 2
  • 30
  • 43
  • Hey thanks for the quick reply! Using your code allowed me to see the default drawable again. However, it doesn't change icons when I tap on it. Is there anything else I need to do to get this to work? Remember that I'm using this drawable for an Action button in a Notification. Could that be the reason why your code doesn't work? Thanks! – NewGradDev Aug 07 '13 at 12:08
  • You might want to add `android:state_focused="true/false"`, but the toggle button work when pressed and I don't know what you have in mind when saying tapped? – g00dy Aug 07 '13 at 12:14
  • I tried `android:state_focused` and that doesn't work either. By "tapped" I mean when the user presses the button. Sorry for the confusion, we're both on the same page now. Will your code work for an action button in a notification? Or does it only work explicitly for the ToggleButton UI element? – NewGradDev Aug 07 '13 at 12:19
  • 1
    It will not work there, but here's a hint for you: Make that button an imagebutton and put in the xml attributes `android:onClick="someFunction"` in that public void someFunction(){} you can check for the currently visualized image and change it. It's that simple. – g00dy Aug 07 '13 at 12:58
  • Ah, I see what you're trying to say. Although I don't understand how I can use the `android:onClick` attribute when `NotificationBuilder` will only allow me to set a drawable for the action button? Here's how I'm setting my action button: `builder.addAction(R.drawable.play_pause_notification_light, "", playPauseTrackPendingIntent);` – NewGradDev Aug 07 '13 at 13:06
  • To do that, you'll need to set-up a new notification with the same ID like the previous one and call `notify()`. – g00dy Aug 07 '13 at 13:31
  • For more information click here -> http://developer.android.com/guide/topics/ui/notifiers/notifications.html – g00dy Aug 07 '13 at 13:32