Cannot get searchview in actionbar to work Cannot get searchview in actionbar to work android android

Cannot get searchview in actionbar to work


Your problem is in your AndroidManifest. I had it almost exactly as you do, because that is what I get following the documentation. But it is unclear or mistaken.

Watching at the API Demos source I found that the "android.app.searchable" metadata must go in your "results" activity, and in the main activity (or the activity where you place the SearchView) you point to the other one with "android.app.default_searchable".

You can see it in the following file, which is the Manifest from my test project:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.test"android:versionCode="1"android:versionName="1.0" ><uses-sdk    android:minSdkVersion="8"    android:targetSdkVersion="15" /><application    android:icon="@drawable/ic_launcher"    android:label="@string/app_name"    android:theme="@style/AppTheme" >    <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.default_searchable"            android:value=".SearchActivity" />    </activity>    <activity        android:name=".SearchActivity"        android:label="@string/app_name" >        <!-- This intent-filter identifies this activity as "searchable" -->        <intent-filter>            <action android:name="android.intent.action.SEARCH" />            <category android:name="android.intent.category.DEFAULT" />        </intent-filter>        <!-- This metadata entry provides further configuration details for searches -->        <!-- that are handled by this activity. -->        <meta-data            android:name="android.app.searchable"            android:resource="@xml/searchable" />    </activity></application>

It is also important that the hint and label in the searchable.xml are references, and not hardcoded strings.

I hope it solves your problem. It took me all day to figure it out :(


Android guides explicitly says:

// Assumes current activity is the searchable activitysearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

Which is in your case NOT TRUE. Because you don't want to search in MainActivity but in SearchableActivity. That means you should use this ComponentName instead of getComponentName() method:

ComponentName cn = new ComponentName(this, SearchableActivity.class);searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));


Also it seems that both the android:hint and android:label attributes MUST be references to strings in strings.xml. Being lazy and hardcoding the string values doesn't seem to work at all :)

i.e. Don't do:

<?xml version="1.0" encoding="utf-8"?><searchable xmlns:android="http://schemas.android.com/apk/res/android"    android:hint="Search for something..."    android:label="My App"></searchable>

But definitely do (as the OP correctly did):

<?xml version="1.0" encoding="utf-8"?><searchable xmlns:android="http://schemas.android.com/apk/res/android"    android:hint="@string/search_hint"    android:label="@string/app_label"></searchable>