14

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!

Chris
  • 3,192
  • 4
  • 30
  • 43
  • possible duplicate of [Cannot get searchview in actionbar to work](http://stackoverflow.com/questions/11699206/cannot-get-searchview-in-actionbar-to-work) – Krizai Sep 08 '15 at 20:05
  • Thank you for the summary of possible sources of errors! I had hard coded strings in searchable. – Michele Dorigatti Aug 06 '20 at 19:38

3 Answers3

12

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>
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
  • Thanks for your reply. The `SearchView` is in the `ActionBar` (see updated question). Since the `MainActivity` has the resources to provide search results, I've added your code to the `MainActivity`-section of the manifest. Still, `searchManager.getSearchableInfo(getComponentName());` returns `null`. – Chris Jan 25 '13 at 08:53
  • Updated the answer. Try to apply yet another the intent-filter as well. – Yaroslav Mytkalyk Jan 25 '13 at 10:10
  • Thanks, it works! Using this other guide http://developer.android.com/guide/topics/search/search-dialog.html, I just figured this out, too :-). – Chris Jan 25 '13 at 10:13
  • This code goes in the manifest, in case somebody is wondering. – Michele Dorigatti Aug 06 '20 at 19:31
2

In my case the problem was my mistake during integration with ActionBarSherlock. In this case android:actionViewClass should be com.actionbarsherlock.widget.SearchView instead of android.widget.SearchView. I know this sounds dumb, but I spent 20 minutes trying to figure out why searchView is null in my code that is similar to yours.

smok
  • 1,658
  • 13
  • 13
  • My issue was the other way around. I had integrated ActionBarSherlock and this was removed in an app update. com.actionbarsherlock.widget.SearchView needed to be removed and replaced with android.widget.SearchView – Craig Champion Apr 28 '15 at 11:51
1

Replace:

android:actionViewClass="android.widget.SearchView"

With:

app:actionViewClass="android.widget.SearchView"

Because app is often used if you are using support library.

cakan
  • 2,099
  • 5
  • 32
  • 42