How to add a SearchWidget to the ActionBar?

Since this question is looked up quite often and I stumbled across the very same problem again and again here is a little follow up that keeps track of all necessary steps to create a SearchWidget.

There is one tricky part about the SearchWidget though: If you use hardcoded Strings in the searchable.xml instead of resources the app will crash with a confusing error message. This took me way too many hours of my life…

  1. Create an Activity to handle the search results

    public class Search extends Activity {}
    
  2. Add a file “searchable.xml” to your res/xml directory (use resources for hint & label!)

    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:hint="@string/search_hint"
        android:includeInGlobalSearch="false"
        android:label="@string/search_label"
        android:searchSettingsDescription="@string/search_global_description" />
    
  3. Create the proper menu item “main.xml” in you res/menu directory

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item
           android:id="@+id/options_menu_main_search"
           android:actionViewClass="android.widget.SearchView"
           android:icon="@drawable/icon_magnifier"
           android:showAsAction="always"
           android:title="Search"/>
    
    </menu>
    
  4. Update your Manifest.xml: Add the search activity and specify which activities may receive search intents. Adding <meta-data android:name="android.app.default_searchable" android:value=".app.Search" /> to an activity node makes it searchable. Adding it to the application node makes all activities searchable.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
        <application>
            <meta-data
                android:name="android.app.default_searchable"
                android:value=".app.Search" />
    
            <activity android:name=".activities.Search" >
                <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
    
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
    </application>
    

  5. Add the SearchManager to your ActionView in every activity that should provide the SearchWidget

    public class Activity extends android.app.Activity
    {
        @Override
        public boolean onCreateOptionsMenu( Menu menu )
        {
            getMenuInflater().inflate( R.menu.main, menu );
    
            // Add SearchWidget.
            SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
            SearchView searchView = (SearchView) menu.findItem( R.id.options_menu_main_search ).getActionView();
    
            searchView.setSearchableInfo( searchManager.getSearchableInfo( getComponentName() ) );
    
            return super.onCreateOptionsMenu( menu );
        }
    }
    

Leave a Comment