Juozas Kontvainis
Juozas Kontvainis

Reputation: 9597

Android Honeycomb - how to show suggestions for a SearchView when query text is empty?

I've added a SearchView to ActionBar of my application, and setup a SuggestionsProvider using SearchableInfo. Suggestions actually work well, except that no suggestions are shown when search query text is empty. I've found that my suggestion provider is not queried for suggestions even when searchSuggestThreshold is set to 0.

The closest I've come to a solution is to use setOnQueryTextFocusChangeListener and in onFocusChange swap the cursor of SearchView suggestion adapter. This way I see a suggestion drop down of correct height, however no suggestion text is shown.

Here's how I query cursor to show suggestions for empty text.

Cursor emptySuggestions = getContentResolver().query(
                            Uri.parse("content://my.authority/search_suggest_query/test"),
                            null, null, null, null);

Is there some way to make suggestions appear correctly?

Of course there's always a possibility to show my own ListPopupWindow when search query is empty, but I'd like to avoid that, unless there's an easy way to reuse layout used in SearchView.

Upvotes: 4

Views: 2709

Answers (2)

user1784450
user1784450

Reputation: 53

In addition to manually trigger onQueryTextChange() method above, SearchView.SearchAutoComplete threshold need to be set to zero

SearchView.SearchAutoComplete sac = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
if (sac != null) {
    sac.setThreshold(0);
}

Upvotes: 1

Juozas Kontvainis
Juozas Kontvainis

Reputation: 9597

I figured out how to trigger search view to query my suggestion provider for empty search text:

    _searchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener()
        {
        @Override
        public void onFocusChange(View v, boolean hasFocus)
            {
            if (!hasFocus)
                return;

            String query = _searchView.getQuery().toString();
            _searchView.setQuery(query, false);
            }
        });

Upvotes: 2

Related Questions