13

I'm trying to get suggestions for SearchView. I've implemented a custom content provider for it. I've also referred to this link to implement suggestions for the SearchView. The problem I'm facing is, I get null value on searchManager.getSearchableInfo(getComponentName())

Here are the snippets:

AndroidManifest.xml

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <provider android:name=".SearchProvider"
        android:enabled="true"
        android:authorities="com.example.currentlocationmapdemo"
        android:grantUriPermissions="true"
        android:exported="true">
        <grant-uri-permission android:pathPattern="*" />
    </provider>
    <uses-library android:name="com.google.android.maps" />

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      <meta-data android:name="android.app.searchable"
          android:resource="@xml/searchable"/> 
    </activity>

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="label"
android:hint="hint"
android:searchSuggestAuthority="com.example.currentlocationmapdemo"
android:searchSuggestSelection=" ? ">

MainActivity

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.searchview_in_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    mSearchView = (SearchView) searchItem.getActionView();
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());  // null returned
mSearchView.setSearchableInfo(info);

 return true;
}
Harshal Kshatriya
  • 5,630
  • 11
  • 44
  • 60

5 Answers5

21

At least one of your activities - the one you're doing the searching from is sufficient - must have this intent-filter in it in the manifest:

<intent-filter>
  <action android:name="android.intent.action.SEARCH" />
</intent-filter>

If not, then searchManager.getSearchableInfo(getComponentName()) always returns null, and your configuration is ignored.

This killed me for a day - I thought it was ActionBarSherlock-related, but no it works fine with that. The problem was that I was trying to short-circuit the sample, as you have too :-)

Kenton Price
  • 5,229
  • 4
  • 23
  • 21
  • 2
    As a note, currently I can ONLY get it to work if the intent-filter is in the current activity. It's pretty aggravating, because I'd like it to go to another activity to display search results. If you know what's up with that, I'd be grateful to hear. – Erhannis Jul 03 '14 at 06:42
  • 1
    Lost hours on this. It was the intent filter missing. – Joao de Araujo Aug 26 '16 at 03:15
10

I think your approach is wrong. You should have 2 activities - 1: Main activity which has as SearchView (on ActionBar or layout) and 2: SearchActivity which will be started when search is performed.

Maybe you can also do it like that but Im not sure. Where would you like to recive ACTION_SEARCH intent in your approach? Normally you do that in OnCreate in your searchActivity like that:

Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  String query = intent.getStringExtra(SearchManager.QUERY);
  System.out.println("searching for: " + query);
}

I did it on two activites and it worked for me. One additional thing I had to do and I didn't find in Android Search Tutorial was adding:

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

In manifest for my MainActivity

Mark
  • 5,466
  • 3
  • 23
  • 24
  • My implementation was actually correct. The problem was a very silly one. I had missed out ' ' in menu.xml. However, the xml file did not show any errors. – Harshal Kshatriya Aug 03 '12 at 03:07
  • 5
    The `default_searchable` attribute is the only thing which is NOT officially documented. Thank you! Drove me crazy. The Android documentation is a joke. – EinLama Nov 18 '13 at 16:19
3

Your searchable.xml contains string literals (hint and label), they should be references. That's what make it fail according to this: SearchInfo always coming out null

Community
  • 1
  • 1
eMich
  • 1,736
  • 1
  • 14
  • 16
0

Just wanted to add for anyone still struggling with this that a <data> tag will interfere with your intent-filter and cause you to get a null searchableInfo. I had this:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <action android:name="android.intent.action.SEARCH" />
    <data android:scheme="https"
          android:host="www.example.com"/>
</intent-filter>

which I needed to change to:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="https"
          android:host="www.example.com"/>
</intent-filter>

<intent-filter>
    <action android:name="android.intent.action.SEARCH" />
</intent-filter>
Evelyn
  • 2,588
  • 3
  • 22
  • 47
0

If you are using two activities, one for displaying search dialog(OtherActivity) and another for results(SearchableActivity), then save yourself time by looking at this https://stackoverflow.com/a/58261225/4594347

Tonui Nicholus
  • 395
  • 2
  • 8