Search code examples
androidactionbarsherlocksearchview

Functioning the searchview in actionbarsherlock


I have a sherlockactivity that implements an onItemClickListener. Then I create a new ListView and put data in it, I want to create a search at the top in the actionbarsherlock like a lot of famous apps.

This is my code:

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
     SearchView searchView = new
             SearchView(getSupportActionBar().getThemedContext());
     menu.add("Search")
             .setActionView(searchView)
             .setShowAsAction(
                     MenuItem.SHOW_AS_ACTION_IF_ROOM
                             | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

     return true;
 }

currently, the word "search" appears at the top of my actionbarsherlock. If I click on it, a textfield will appear in the actionbarsherlock. But when I type anything or click search (In the keyboard) nothing happens. What am I missing?


Solution

  • These two are the methods you need to override to control when the user types on the SearchView and when the user clicks on the search button:

    public class MyClass extends SherlockActivity implements SearchView.OnQueryTextListener{
    ...
    
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }
    
        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    }
    

    Then you need

    searchView.setOnQueryTextListener(this);