I am using Jake Wharton\'s excellent ActionBarSherlock library and have a collapsible search action view. I want to popup the soft keyboard when the search action view is ex
For an easier implementation of a collapsible search item in your ActionBar, you may simply use SearchView which is also available in ActionBarSherlock (com.actionbarsherlock.widget.SearchView).
It comes with many handy methods and handles showing/hiding the software keyboard on expanding/collapsing the view automatically.
Plus, it is easier to define what to do on submitting the search. Just take a look at all the listeners you can set on that SearchView.
Here's an example:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SearchView searchView = new SearchView(this);
searchView.setQueryHint(getString(R.string.search_hint));
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// what to do on submit, e.g. start an Activity and pass the query param
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
menu.add(getString(R.string.search_title))
.setIcon(R.drawable.ic_action_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM|MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
// ...place to add other menu items
return super.onCreateOptionsMenu(menu);
}
Another good reason: I had some layout issues on older devices when using a custom EditText as my search view (like you did in your question). The default SearchView solved all these issues and saved me many lines of code.