1

I have an action bar which contains an item shuffle_action.

I have used below case in the onOptionsItemSelected(MenuItem item) function.

 switch (item.getItemId()){
    case R.id.action_shuffle:
                    if(MusicService.shuffle) {
                        item.setChecked(false);
                        Toast.makeText(this,String.valueOf(item.isChecked()),
                        Toast.LENGTH_SHORT).show();// returns false
                        musicService.clearShuffle();
                    }
                    else {
                        item.setChecked(true);
                        Toast.makeText(this,String.valueOf(item.isChecked()),
                        Toast.LENGTH_SHORT).show();// returns true
                    }
                    musicService.setShuffle();
                    break;
    }

Now this thing works perfectly as i want it to work.

<item
        android:id="@+id/action_shuffle"
        android:icon="@drawable/shuffle_drawable"
        android:title="Shuffle"
        app:showAsAction="ifRoom"
        android:checkable="true"
        android:checked="false"
        />

shuffle_drawable.xml

<?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/shuffle_pressed"/>
    <item
        android:state_checked="false"
        android:drawable="@drawable/ic_shuffle_black_18dp"/>
</selector>

The thing is that when the state changes the drawable won't change.Why is this happening?

Tilak Raj
  • 1,369
  • 5
  • 31
  • 64
  • 1
    https://stackoverflow.com/questions/6683186/menuitems-checked-state-is-not-shown-correctly-by-its-icon – Faizan Mubasher Dec 20 '17 at 04:44
  • yeah did that after half an hour of posting this question.Thanks though – Tilak Raj Dec 20 '17 at 05:04
  • Possible duplicate of [MenuItem's checked state is not shown correctly by its icon](https://stackoverflow.com/questions/6683186/menuitems-checked-state-is-not-shown-correctly-by-its-icon) – Tilak Raj Dec 20 '17 at 05:05
  • You can check my answer as well : https://stackoverflow.com/questions/31400870/add-toggle-button-to-the-menu-to-stop-and-start-service/34760443#34760443 – Faizan Mubasher Dec 20 '17 at 06:29

0 Answers0