title: Android - Implement search bar with one click
body: <p>I'm trying to implement a search bar on the main Activity of my app. For that, I used the documentation from the <a href="http://developer.android.com/training/search/setup.html" rel="nofollow noreferrer">android developer website</a>.</p>
  
  <p>I followed the instructions, and I have my search bar where I can put some query. The problem here is that for the few first time when I try to make a search, I have to click 2 times on a search icon. </p>
  
  <p>First, my bar looks like a default one : 
  <a href="https://i.stack.imgur.com/1URnQ.png" rel="nofollow noreferrer">Search bar by default</a></p>
  
  <p>I notice that the icon is not clearly white, it's like there is some transparency in it, but I've selected a full white icon. </p>
  
  <p>Then after a click on the icon, my bar looks like this : 
  <a href="https://i.stack.imgur.com/CpYnQ.png" rel="nofollow noreferrer">Search bar clicked one time</a></p>
  
  <p>The icon goes to the left and I have a back button as I was already in the search field. </p>
  
  <p>When I click on the search icon again, I finally have the result that I want : 
  <a href="https://i.stack.imgur.com/SLRkL.png" rel="nofollow noreferrer">Search bar in edit mode</a></p>
  
  <p>When I quit the search bar and do it again, it's the same for 1 or 2 times, and then, I have the normal behavior, I go from the first image to the last just with one click. </p>
  
  <p>Here's my files : </p>
  
  <p>menu_app.xml</p>
  
  <pre><code>&lt;menu xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  &lt;item android:id="@+id/search"
      android:title="@string/search"
      android:icon="@drawable/ic_search"
      app:showAsAction="collapseActionView|ifRoom"
      app:actionViewClass="android.widget.SearchView" /&gt;
  &lt;/menu&gt;
  </code></pre>
  
  <p>AndroidManifest.xml</p>
  
  <pre><code>    &lt;activity
          android:name=".activities.MainActivity"
          android:label="@string/app_name"
          android:theme="@style/AppTheme.NoActionBar"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
              &lt;action android:name="android.intent.action.SEARCH"/&gt;
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
  
          &lt;meta-data android:name="android.app.searchable"
              android:resource="@xml/searchable"/&gt;
      &lt;/activity&gt;
  </code></pre>
  
  <p>searchable.xml</p>
  
  <pre><code>&lt;searchable xmlns:android="http://schemas.android.com/apk/res/android"
  android:label="@string/app_name"
  android:hint="@string/search"/&gt;
  </code></pre>
  
  <p>OnCreateOptionsMenu in MainActivity.java </p>
  
  <pre><code>    @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu_main, menu);
  
      // Associate searchable configuration with the SearchView
      SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
      final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
  
      searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
          @Override
          public boolean onQueryTextSubmit(String s) {
              Log.d(TAG, s);
              searchView.clearFocus();
              return true;
          }
  
          @Override
          public boolean onQueryTextChange(String s) {
              Log.d(TAG, s);
              return false;
          }
      });
  
      return true;
  }
  </code></pre>
  
  <p>Thanks in advance for your help!</p>