Question

I have a search widget within the action bar of my main activity. I would like to display suggestions after the user type at least 2 characters.

I'm aware this can be done by setting android:searchSuggestThreshold="2", but it doesn't seem to work because the SuggestionContentProvider is called when the user types zero or many characters.

Even when the user clicks on the search icon (0 characters) to expand the widget, the ContentProvider is called.

Here is my searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint"
    android:searchSettingsDescription="@string/search_settings"
   android:searchSuggestAuthority="com.studiofrenetic.rmfb.Services.SearchContentProvider"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:searchSuggestThreshold="2">
</searchable>

Only one searchable activity in my Manifest.xml:

<activity
    android:name="com.studiofrenetic.rmfb.MainActivity"
    android:screenOrientation="portrait"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
                android:resource="@xml/searchable" />
</activity>

Code to set SearchableInfo in onCreateOptionsMenu:

MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

mSearchView.setOnQueryTextListener(this);

// Assumes current activity is the searchable activity
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.setIconifiedByDefault(true);

I use ActionBarCompat (with minSdkVersion 14 and targetSdkVersion 19).

I search across the web and nobody seems to have this strange behavior... Please help me to understand the problem.

Thanks in advance :)

Was it helpful?

Solution

Finally I had a misunderstanding concerning the behavior related to searchSuggestThreshold:

It's normal that SuggestionContentProvider is called on every typed character, but the uri parameter doesn't contain the query until the threshold is reached.

So, the code related to suggestions generation should be skipped.

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