4

I'm currently working on adding search ability to my app but I'm facing some problems. First of all I'd just like to clear out that I don't want to create a search activity, I just want to use the Action Bar SearchView and press on suggestions to perform the search.

I've created a SearchView which expands correctly although it doesn't seem like it can attach to my Searchable Configuration xml object. I added this line of code:

android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"

Yet my SearchView doesn't seem to be affected by it. The same goes for this line of code:

android:hint="Search my stuff"

Again, no effect.

Here's my activity in my AndroidManifest.xml:

<activity
    android:name="com.simon.holocountownapp.ItemListActivity"
    android:label="Holo Countdown" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

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

Here's my setup of the SearchView in my onCreateOptionsMenu():

searchView = (SearchView) menu.findItem(R.id.abSearch)
            .getActionView();

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));

searchView.setIconifiedByDefault(false);
searchView.setQueryRefinementEnabled(true);

So to summarise things: The tags used to my searchable.xml doesn't seem to attach to my SearchView, what am I doing wrong?

Thanks for taking the time to read this, I hope you can help me :)

SweSnow
  • 17,504
  • 10
  • 36
  • 49

3 Answers3

2

You need to add the android.intent.action.SEARCH intent filter as well. See the code here for reference.

 <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
  </intent-filter>
Akash
  • 631
  • 1
  • 8
  • 16
  • I added that but it's still not working. My original post has been updated. – SweSnow Feb 16 '13 at 16:34
  • Is the `onCreateOptionsMenu()` that you posted of the `com.simon.holocountownapp.ItemListActivity` Activity. `getComponentName()` will return the name of the current Activity. It should be the name of the activity which has the android.intent.action.SEARCH intent filter. – Akash Feb 16 '13 at 18:44
  • Yes it is, I double checked just now – SweSnow Feb 16 '13 at 19:46
  • 1
    @Akash: The tutorials have the searchable activity as a separate one from the main activity, but the activity where the SearchView is displayed (the main activity, in my case) is where the `metadata` is supposed to go. How would I go about getting the component name of the searchable activity from the main one? – RobH Jul 16 '14 at 02:42
0

I think you also need a <meta-data> element as a child of the <application> element

<application>
        <meta-data
            android:name="android.app.default_searchable"
            android:value="com.simon.holocountownapp.ItemListActivity" >
        </meta-data>
</application>

I don't recall if this is documented. I think I found it in one of the sample applications.

Dan
  • 791
  • 7
  • 11
0

I had the same problem, seems that I was missing the default searchable meta-data tag in the manifest.

Add this where you declare the Activity that sends the search to the searchable Activity:

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

Your manifest should look like this:

<application ... >
<!-- this is the searchable activity; it performs searches -->
<activity android:name=".SearchableActivity" >
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>
</activity>

<!-- this activity enables the search dialog to initiate searches
     in the SearchableActivity -->
<activity android:name=".OtherActivity" ... >
    <!-- enable the search dialog to send searches to SearchableActivity -->
    <meta-data android:name="android.app.default_searchable"
               android:value=".SearchableActivity" />
</activity>
...

docs

Periklis Vai
  • 1,395
  • 2
  • 11
  • 19