Question

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?

Was it helpful?

Solution 3

You always have the option of differentiating your solution depending on the current running version:

int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
// pre honeycomb
} else {
// honeycomb and post
}

I know this might not be exactly what you are looking for but it might get you some of the way .

OTHER TIPS

This works on API level 10 for expanding ActionView e.g. SearchView

MenuItemCompat.expandActionView(mSearchMenuItem);

You can use MenuItemCompat static methods. Example:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search, menu);

    MenuItem menuItem = menu.findItem(R.id.action_search);
    MenuItemCompat.expandActionView(menuItem);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    ...
}

More info: http://developer.android.com/reference/android/support/v4/view/MenuItemCompat.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top