Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: saerchable.xml and searchSuggestThreshold parameter

I have a search function in my project. In the searchable.xml, i would like to limit the search only when user types at least 3 characters.

This can be done with android:searchSuggestThreshold="3" but in my case, nothing happens, the user can still perform a search when he types on or more characters.

Here is my searchable.xml:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/Label"
    android:hint="@string/Hint"
    android:searchSuggestThreshold="3"
    android:includeInGlobalSearch="true"/>

Here is the 2 search activityes from the Manifest

 <activity android:name=".Search"
        android:configChanges="orientation|keyboardHidden">
          <intent-filter>   
        <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>   
        </activity>

         <activity android:name=".SearchInterface"
        android:configChanges="orientation|keyboardHidden">
         <meta-data android:name="android.app.default_searchable"
               android:value=".Search" />
         </activity>
like image 213
Milos Cuculovic Avatar asked Jun 04 '12 07:06

Milos Cuculovic


1 Answers

Before answer your question there is one thing you have to change in the AndroidManifest.

 <meta-data android:name="android.app.default_searchable"
               android:value=".Search" />

have to be between <application></application> tags but not inside <activity></activity> ones. Because it declares the default activity to call when a Search intent is fired. After that try if the android:searchSuggestThreshold now works. What you can do as a workaround is in the SearchHelper you are using (the class that manage sql queries) you recieve the search input from the user as the parameter so you can put a simple if. Like this:

public static Cursor getAutoCompleteCursor(String s) {
    Cursor cursor;
    if (s != null && s.length() > 2)
        cursor = searchAutoComplete(s);
    else
        cursor = null;
    return cursor;
}

I hope you understand what I mean. Any doubt just let me know!

like image 200
rallat Avatar answered Nov 14 '22 01:11

rallat