title: Changing Action Bar Menu Item Icon When Pressed in Android Studio
body: <p>I am working on this simple Android application. I have three Menu Items on the Action Bar and each of the menu items inflate 3 different fragments when clicked on them. </p>
  
  <p>My Question: When I click on one of the Menu Items, I want that Menu Item's icon changing to another icon in the drawables folder and STAY like that until I click on another Menu Item and then it will change to the first icon before I clicked on it. </p>
  
  <p>I tried to use a selector .xml with "state_pressed" and when I click on the Menu Item it changes the icon for a second but then it goes back to the first icon as soon as I stop clicking. You can find the related codes below. I'd appreciate it if you could help me with this one.</p>
  
  <p><strong>submenu.xml</strong> <em>(Action Bar Menu):</em></p>
  
  <pre><code>&lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools" &gt;
      &lt;item
          android:id="@+id/action_todo"
          android:title=""
          android:icon="@drawable/action_bar_selector"
          app:showAsAction="always" /&gt;
  &lt;/menu&gt;
  </code></pre>
  
  <p><strong>action_bar_selector.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;item
          android:drawable="@drawable/ic_check_box_white_24dp"
          android:state_pressed="true" /&gt;
      &lt;item
          android:drawable="@drawable/ic_check_box_white_24dp_unpressed" /&gt;
  &lt;/selector&gt;
  </code></pre>
  
  <p><strong>ProjectTodo.java</strong> <em>(Inflating Action Bar in Fragment Class):</em></p>
  
  <pre><code>public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
      inflater.inflate(R.menu.submenu, menu);
  }
  
  public boolean onOptionsItemSelected(MenuItem item){
      int id = item.getItemId();
      if(id == R.id.action_back){
          ProjectDetails frag = new ProjectDetails();
          ProjectMainListFragmentChanger fragInterface = (ProjectMainListFragmentChanger) getActivity();
          fragInterface.projectMainListChangeFragment(frag);
          return false;
      }
  
      if(id == R.id.action_details){
          return true;
      }
  
      if(id == R.id.action_todo){
          ProjectDetails frag = new ProjectDetails();
          ProjectTodoFragmentChanger fragInterface = (ProjectTodoFragmentChanger) getActivity();
          fragInterface.projectTodoChangeFragment(frag);      
          return false;
      }
      return super.onOptionsItemSelected(item);
  }
  </code></pre>
  
  <p>You can see a quick .gif image of the current application. When I click on the Menu Item, it only changes to the 2nd icon for a second and then goes back to its original icon:</p>
  
  <p><a href="https://i.stack.imgur.com/q0P72.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q0P72.gif" alt="enter image description here"></a></p>