Two-state action button in Jelly Bean Notifications

704 Views Asked by At

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!

1

There are 1 best solutions below

7
On

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>