title: Swap/Switch Android ActionBar items in onOptionsItemSelected method
body: <p>How can I swap/switch between two <code>ActionBar</code> items?</p>
  
  <p>I currently have two items in my action bar as indicated in the first image below - see the "grid" and the "timeline" icons. </p>
  
  <p>I want to show only one and when the user touches  it, it should swap out with the other one. So say the grid item is showing, and the user touches it, the timeline item should show and the grid item should disappear. Like in the second image.</p>
  
  <p><img src="https://i.stack.imgur.com/6t0JH.jpg" alt="enter image description here"></p>
  
  <p>I've tried this, but it doesn't seem to work - no swapping is happening. </p>
  
  <pre><code>private static final int SHOW_AS_ACTION_NEVER = 0;
  private static final int SHOW_AS_ACTION_IF_ROOM = 1;
  private static final int SHOW_AS_ACTION_ALWAYS = 2;
  
  private Menu menu;
  private MenuItem itemGrid;
  private MenuItem itemTimeline;
  
  //...
  
  @Override
  public boolean onCreateOptionsMenu(Menu m) {
          // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.actionbar, m);
  
      menu = m;
  
      itemTimeline = menu.findItem(R.id.action_timeline);
      itemTimeline.setShowAsAction(SHOW_AS_ACTION_NEVER);
  
      return true;
      }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem mi) {
      switch (mi.getItemId()) {
      case R.id.action_timeline:
          Toast.makeText(this, "Action grid selected", Toast.LENGTH_SHORT).show();
  
          itemTimeline = menu.findItem(R.id.action_timeline);
          itemTimeline.setShowAsAction(SHOW_AS_ACTION_NEVER);             
  
          itemGrid = menu.findItem(R.id.action_grid);
          itemGrid.setShowAsAction(SHOW_AS_ACTION_ALWAYS);
  
          break;
      case R.id.action_settings:
          Toast.makeText(this, "Action settings selected", Toast.LENGTH_SHORT).show();
          break;
  
          default:
          break;
          }
  
      return true;
      }
  </code></pre>