private void updateMenu() { if (menu != null) { if (gridView.getAdapter() instanceof SearchAdapter) return; menu.clear(); getMenuInflater().inflate(R.menu.roster, menu); menu.findItem(R.id.add).setEnabled(service.isAuthenticated()); menu.findItem(R.id.notes).setEnabled(service.isAuthenticated()); menu.findItem(R.id.disco).setEnabled(service.isAuthenticated()); menu.findItem(R.id.offline) .setTitle( prefs.getBoolean("hideOffline", false) ? R.string.ShowOfflineContacts : R.string.HideOfflineContacts); MenuItem sound = menu.findItem(R.id.notify); sound.setShowAsActionFlags( prefs.getBoolean("showSound", false) ? MenuItem.SHOW_AS_ACTION_ALWAYS : MenuItem.SHOW_AS_ACTION_NEVER); if (prefs.getBoolean("soundDisabled", false)) { sound.setTitle(R.string.EnableSound); sound.setIcon(R.drawable.ic_menu_sound_off); } else { sound.setTitle(R.string.DisableSound); sound.setIcon(R.drawable.ic_menu_sound_on); } MenuItem.OnActionExpandListener listener = new MenuItem.OnActionExpandListener() { @Override public boolean onMenuItemActionCollapse(MenuItem item) { gridView.setAdapter(null); searchString = null; updateList(); updateMenu(); return true; } @Override public boolean onMenuItemActionExpand(MenuItem item) { gridView.setAdapter(searchAdapter); searchString = ""; updateList(); return true; } }; SearchView searchView = new SearchView(this); searchView.setQueryHint(getString(android.R.string.search_go)); searchView.setSubmitButtonEnabled(false); searchView.setOnQueryTextListener( new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextChange(String newText) { searchString = newText; updateList(); return true; } @Override public boolean onQueryTextSubmit(String query) { return true; } }); MenuItem item = menu.findItem(R.id.search); item.setActionView(searchView); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); item.setOnActionExpandListener(listener); super.onCreateOptionsMenu(menu); } }