I would be grateful if someone shares the code to change the icon in the ActionBar (menu) by clicking on it (selected / not selected).

<menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/ic_favorites_unchecked" android:icon="@drawable/ic_menu_favorites_unchecked" app:showAsAction="always" android:title="Избранное"> </item> <item android:id="@+id/ic_favorites_checked" android:icon="@drawable/ic_menu_favorites_checked" app:showAsAction="always" android:title="Избранное"> </item> </menu> 

How to implement it with:

 public boolean onPrepareOptionsMenu(final Menu menu) 

and

 public boolean onOptionsItemSelected(MenuItem item) 

    2 answers 2

    1) You need to get a boolean in your SharedPreferences to your buttons.

    2) In public boolean onOptionsItemSelected(MenuItem item) you need to find your items, check the value of the above variable and assign to the elements accordingly

    menuItem.setChecked (boolean checked);

    3) Well, save to the notorious variable new value.

    Something like this.

    4) Well, in the XML menu it is necessary to prescribe that they are selectable, these elements. Those. wrap them up

      <group android:checkableBehavior="single" > <!-- тут кнопки --> </group> 
    • Thanks for the tip about group - Chekist
    • You are welcome ) - JuriySPb

    This code turned out, maybe someone will come in handy (there is not enough reputation to mark your answer as correct):

     String name = "Какое-то имя"; boolean addedToFavorites = false; // Не добавлен в избранное @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == R.id.ic_favorites_unchecked){ Toast.makeText(this, name + " добавлен(а) в избранное", Toast.LENGTH_SHORT).show(); invalidateOptionsMenu(); } else{ Toast.makeText(this, name + " удален(а) из избранного", Toast.LENGTH_SHORT).show(); invalidateOptionsMenu(); } return super.onOptionsItemSelected(item); } @Override public boolean onPrepareOptionsMenu(final Menu menu) { if (!addedToFavorites) { MenuItem mi = menu.findItem(R.id.ic_favorites_checked); menu.removeItem(R.id.ic_favorites_checked); mi.setIcon(R.drawable.ic_menu_favorites_unchecked); mi.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM); addedToFavorites = true; } else { MenuItem mi = menu.findItem(R.id.ic_favorites_unchecked); menu.removeItem(R.id.ic_favorites_unchecked); mi.setIcon(R.drawable.ic_menu_favorites_checked); mi.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM); addedToFavorites = false; } return super.onPrepareOptionsMenu(menu); } 

    And the XML itself:

     <?xml version="1.0" encoding="utf-8"?> <menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single" > <item android:id="@+id/ic_favorites_unchecked" android:icon="@drawable/ic_menu_favorites_unchecked" app:showAsAction="always" android:title="Избранное"> </item> <item android:id="@+id/ic_favorites_checked" android:icon="@drawable/ic_menu_favorites_checked" app:showAsAction="always" android:title="Избранное"> </item> </group> </menu> 
    • Administrators, please mark my own answer as correct. Thank. - Chekist