title: Actionbar - hiding all menu items except search field
body: <p>I have a menu in my actionbar with two items. After opening search view "a piece of pen" is still displayed. How can I hide it?</p>
  
  <p>menu.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
  
      &lt;item
          android:id="@+id/search"
          android:title="@string/search"
          android:icon="@drawable/ic_search"
          app:actionViewClass="android.widget.SearchView"
          app:showAsAction="ifRoom|withText" /&gt;
  
      &lt;item
          android:id="@+id/edit"
          android:icon="@drawable/ic_edit"
          android:title="@string/edit"
          app:showAsAction="ifRoom|withText" /&gt;
  
  &lt;/menu&gt;
  </code></pre>
  
  <p>Actionbar:
  <a href="https://i.stack.imgur.com/6A9AK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/6A9AK.png" alt="search closed"></a></p>
  
  <p>Actionbar with search view opened:
  <a href="https://i.stack.imgur.com/q4LH5.png" rel="noreferrer"><img src="https://i.stack.imgur.com/q4LH5.png" alt="search open"></a></p>
  
  <p>I tried also:</p>
  
  <pre><code>app:showAsAction="collapseActionView|ifRoom"
  </code></pre>
  
  <p>But in this case I have:</p>
  
  <p><a href="https://i.stack.imgur.com/6ot2d.png" rel="noreferrer"><img src="https://i.stack.imgur.com/6ot2d.png" alt="collapseActionView"></a></p>
  
  <p>I want to get the same what is presented in the second picture. But edit icon should be hidden or eventually displayed but as a whole.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Based @Ahmad Alsanie answer I wrote sth like this:</p>
  
  <pre><code>SearchView view = (SearchView) menu.findItem(R.id.search).getActionView();
  view.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
      @Override
      public void onFocusChange(View v, boolean hasFocus) {
      if (hasFocus) 
          menu.findItem(R.id.edit).setVisible(false);
  });
  </code></pre>
  
  <p>And it works, but how can I restore edit icon after closing search view? I tried <code>setOnCloseListener</code> but unsuccessfully.</p>