Change icons in ActionBar dynamically

You’ll have to save off a reference to the MenuItem after doing the inflation. So something like the following:

public boolean onCreateOptionsMenu( Menu menu ) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate( R.menu.actionbarlogic, menu );
    playMenu = menu.findItem(R.id.playMenu);
    updatePlayStatus();
    return menu;
}

public void updatePlayStatus() {
    if( playService.isConnected() ) {
        playService.isPlaying() ? playMenu.setIcon(R.drawable.pause) : playMenu.setIcon(R.drawable.play);
    }
}

Then you can refer to the playMenu anytime. So you can modify the item as say your player finishes playing and should go back to a play icon.

Leave a Comment