@Override
 public void onBackPressed() {
   if (mSearchView.isSearching()) {
     mSearchView.closeSearch();
   } else if (mRecyclerView.getVisibility() == View.VISIBLE) {
     mResultAdapter.clear();
     mRecyclerView.setVisibility(View.GONE);
   } else {
     super.onBackPressed();
   }
 }
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
     ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
     mSearchView.populateEditText(matches);
   }
   super.onActivityResult(requestCode, resultCode, data);
 }
 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_menu_item_sample);
   mSearchView = (PersistentSearchView) findViewById(R.id.searchview);
   VoiceRecognitionDelegate delegate =
       new DefaultVoiceRecognizerDelegate(this, VOICE_RECOGNITION_REQUEST_CODE);
   if (delegate.isVoiceRecognitionAvailable()) {
     mSearchView.setVoiceRecognitionDelegate(delegate);
   }
   mToolbar = (Toolbar) findViewById(R.id.toolbar);
   mSearchTintView = findViewById(R.id.view_search_tint);
   this.setSupportActionBar(mToolbar);
   ActionBar actionBar = getSupportActionBar();
   if (actionBar != null) {
     actionBar.setDisplayShowHomeEnabled(true);
     actionBar.setDisplayHomeAsUpEnabled(true);
   }
   setUpSearchView();
 }
  public void setUpSearchView() {
    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview_search_result);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mResultAdapter = new SearchResultAdapter(new ArrayList<SearchResult>());
    mRecyclerView.setAdapter(mResultAdapter);
    mSearchTintView.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            mSearchView.cancelEditing();
          }
        });
    mSearchView.setHomeButtonListener(
        new HomeButtonListener() {

          @Override
          public void onHomeButtonClick() {
            // Hamburger has been clicked
            Toast.makeText(MenuItemSampleActivity.this, "Menu click", Toast.LENGTH_LONG).show();
          }
        });
    mSearchView.setSuggestionBuilder(new SampleSuggestionsBuilder(this));
    mSearchView.setSearchListener(
        new SearchListener() {

          @Override
          public boolean onSuggestion(SearchItem searchItem) {
            Log.d("onSuggestion", searchItem.getTitle());
            return false;
          }

          @Override
          public void onSearchEditOpened() {
            // Use this to tint the screen
            mSearchTintView.setVisibility(View.VISIBLE);
            mSearchTintView
                .animate()
                .alpha(1.0f)
                .setDuration(300)
                .setListener(new SimpleAnimationListener())
                .start();
          }

          @Override
          public void onSearchEditClosed() {
            mSearchTintView
                .animate()
                .alpha(0.0f)
                .setDuration(300)
                .setListener(
                    new SimpleAnimationListener() {
                      @Override
                      public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        mSearchTintView.setVisibility(View.GONE);
                      }
                    })
                .start();
          }

          @Override
          public boolean onSearchEditBackPressed() {
            if (mSearchView.isEditing()) {
              mSearchView.cancelEditing();
              return true;
            }
            return false;
          }

          @Override
          public void onSearchExit() {
            mResultAdapter.clear();
            if (mRecyclerView.getVisibility() == View.VISIBLE) {
              mRecyclerView.setVisibility(View.GONE);
            }
          }

          @Override
          public void onSearchTermChanged(String term) {}

          @Override
          public void onSearch(String string) {
            Toast.makeText(MenuItemSampleActivity.this, string + " Searched", Toast.LENGTH_LONG)
                .show();
            mRecyclerView.setVisibility(View.VISIBLE);
            fillResultToRecyclerView(string);
          }

          @Override
          public void onSearchCleared() {}
        });
  }
 public void openSearch() {
   View menuItemView = findViewById(R.id.action_search);
   mSearchView.setStartPositionFromMenuItem(menuItemView);
   mSearchView.openSearch();
 }