I am implementing a search interface on my application using the search widget. I do not plan to support any API level < 11 (Honeycomb). In the search activity, I'd like to override onKeyDown()
to expand the search widget when user presses the search key, and minimize it when the query text is submitted.
This is easily accomplished by calling:
@Override
public boolean onSearchRequested() {
if (mSearchMenuItem != null) {
mSearchMenuItem.expandActionView();
}
return false;
}
and when the query is submitted:
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
//Do nothing, results for the string supplied are already shown.
//Just collapse search widget
mSearchMenu.collapseActionView();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
//do other stuff
return false;
});
The problem is that both collapse and expand were introduced in API level 14. As mentioned earlier I want to provide support as far back as API level 11, and I'm having trouble achieving the same functionality without calling the new API methods.
I tried:
@Override
public boolean onSearchRequested() {
searchView.setIconifiedByDefault(false);
//and also called
searchView.requestFocusFromTouch();
}
but that simply didn't do anything at all (by anything I mean the searchwidget remains collapsed if it already was.
Since I am keeping the reference to the menuItem, I also tried:
@Override
public boolean onSearchRequested() {
SearchActivity.this.onOptionsItemSelected(mSearchMenuItem);
}
but that didn't do anything at all either, last thing I tried was:
@Override
public boolean onSearchRequested() {
searchMenu.getActionView().performClick();
return false;
}
But that is no-go as well.
Any ideas on what I could do to provide the same functionality as menuItem.collapseActionView()
and menuItem.expandActionView()
without having to sacrifice compatibility?