searchManager.getSearchableInfo(getComponentName()) returns null

Chris picture Chris · Dec 17, 2012 · Viewed 9.6k times · Source

I've followed this Android guide in order to add a search bar to an activity. The setup looks like this:

res/menu/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/search"
    android:title="@string/menu_search"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="collapseActionView|ifRoom"
    android:actionViewClass="android.widget.SearchView" />
<item
    android:id="@+id/menu_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/menu_settings"
    android:icon="@drawable/ic_menu_settings"/>

</menu>

res/xml/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" />

Activity entry in manifest file:

    <activity
        android:name="de.ivu.realtime.app.activity.MainActivity"
        android:label="@string/app_name" >
        <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" />
        <meta-data
            android:name="android.app.default_searchable"
            android:value="de.ivu.realtime.app.activity.MainActivity" />
    </activity>

and the code in MainActivity:

@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    this.getMenuInflater().inflate(R.menu.activity_main, menu);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
        searchView.setSearchableInfo(searchableInfo);
    }

    return true;

The call to searchManager.getSearchableInfo(getComponentName()); always returns null. I've seen this in other questions on stackoverflow, however, none of the solutions presented there seem to apply to my case.

  • the menu XML has a <?xml version="1.0" encoding="utf-8"?> header
  • the searchable XML has no hard-coded strings
  • renaming the menu item does not work
  • cleaning and rebuilding does not do the trick either

Any help is very much appreciated. Thanks!

Answer

Yaroslav Mytkalyk picture Yaroslav Mytkalyk · Jan 10, 2013

Add it to the activity where you've put your SearchView

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

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