code: @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          super.onCreateOptionsMenu(menu, inflater);
          if (userSearch) {
              inflater.inflate(R.menu.compose, menu);
  
              SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
  
              searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                  @Override
                  public boolean onQueryTextSubmit(String query) {
                      return false;
                  }
  
                  @Override
                  public boolean onQueryTextChange(String newText) {
                      filter(newText);
                      return true;
                  }
              });
          }
      }
docs: Search menu