67

I am pretty new to java and android development; and I have been working on an app in which I want an action bar with a SearchView. I have looked at the google examples but I can not get it to work. I must be doing something wrong and I'm about to give up on app-development :D I have searched but I have not found anything helpful. Maybe you guys can help me :)

The problem: I get the search view to open as it should, but after that nothing happens at all, I've noticed that my searchManager.getSearchableInfo(getComponentName()) returns null. Also my hint that I've given is not displayed in my search box which leads me to believing that maybe the app can't find my searchable.xml? Enough talk :)

MainActivity.java

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

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

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
            (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

        return true;
    }
}

searchable.xml

<?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>

Androidmanifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.searchapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

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

        <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>           
        </activity>

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

    </application>

</manifest>

SearchableActivity.java

package com.example.searchapp;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class SearchableActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);

        // Get the intent, verify the action and get the query
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          doMySearch(query);
        }
    }

    private void doMySearch(String query) {
        Log.d("Event",query);
    }
}

And here is a link to the referred project, I would be eternally grateful if someone was to help me with this!

Source code

3

9 Answers 9

149

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 :(

13
  • 5
    It totally worked! So it wasn´t just me then I guess :) I'm one happy camper now, thanks Sloy! :) Jul 29, 2012 at 6:20
  • 6
    This post just saved us - the missing part was <meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" /> Dec 1, 2012 at 20:15
  • 1
    It is the Android Dev documentation that is unclear and this answer saved me from implementing lots of code. Thanks!
    – jaibatrik
    Mar 30, 2013 at 18:17
  • 6
    It is also important that the hint and label in the searchable.xml are references, and not hardcoded strings. .. that fixed my problem. This is very ridiculous bug. Jun 14, 2013 at 18:57
  • 3
    I had the same problem and figured out what to do. It took me longer than I expected since the official doc has several mistakes. It has been asked multiple times in StackOverflow–I found at least 5 posts asking the same question. I flagged them as duplicates to this one. I also wrote a blog to summarize what I learnt. Hope it helps.
    – Clint
    Jul 3, 2014 at 22:23
83

Android guides explicitly says:

// Assumes current activity is the searchable activity
searchView.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));
6
  • I did the cut'n'paste and did not realize the docs said this. Thanks!
    – Ray Hunter
    Apr 4, 2015 at 21:11
  • thaks! It's really the mistake I made
    – Randy
    Apr 27, 2015 at 5:32
  • help here stackoverflow.com/questions/32197791/… Aug 26, 2015 at 6:03
  • This documentation catch also got me. Thank you for help, this advice works great with Xamarin, the only change is that you have to use following C# code var componentName = new ComponentName(this, Java.Lang.Class.FromType(typeof(SearchView))) Sep 1, 2015 at 18:56
  • 1
    This solves the problem but the search icon in the keypad is replaced by an arrow now. How to get it back?
    – Iqbal
    Feb 22, 2016 at 12:15
12

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>
2
  • 4
    Is this a bug or is this some specific technological restriction? To me, it seems to be a bug (it isn't mentioned in documentation that you can't use hardcoded strings). Jun 14, 2013 at 19:43
  • 2
    Thanks for this. I wasted 2 days before figuring this out.
    – lahsrah
    Jun 25, 2016 at 2:09
7

If you are using Android Studio,

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

part may not work.

write full package name like

<meta-data
                android:name="android.app.default_searchable"
                android:value="com.example.test.SearchActivity" />
2
  • 1
    No this should not be an issue. I am using Android Studio (version 0.8.1) and it works fine with .SearchActivity.
    – Clint
    Jul 3, 2014 at 22:18
  • This solve my problem, with android studio 2.3.3 and build tools 25.0.2
    – sonic
    Aug 31, 2017 at 20:33
7

be careful with "xml/searchable.xml". If you made HARD CODE with android:hint and android:label.It will not work. Haha Should be :

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/app_name"
android:label="@string/search_lb"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>
1
2

There's actually no need use the "missing part" as described in the answers above. This method(using a SearchView) works exactly as the official docs say till Using the Search widget. Here a slight modification needs to made as follows:

Properties in a SearchableInfo are used to display labels, hints, suggestions, create intents for launching search results screens and controlling other affordances such as a voice button(docs).

Thus, the SearchableInfo object we pass to the SearchView.setSearchableInfo(SearchableInfo s) must contain proper references to the target activity which should be displayed to show the search results. This is done by manually passing the ComponentName of our target activity. One constructor for creating a ComponentName is

public ComponentName (String pkg, String cls)

pkg and cls are package names and class names of the target activity to be launched for displaying search results. Note that:

pkg must point to the root package name of your project and,

cls must be a fully qualified name of the class(i.e. the entire thing)

e.g.:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    // Get the SearchView and set the searchable configuration
String pkg = "com.example.musicapp"
String cls = "com.example.musicapp.search.SearchActivity"
ComponentName mycomponent = new ComponentName(pkg,cls);       
SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(mycomponent));
searchView.setIconifiedByDefault(false);   //if using on actionbar
return true;
}

Depending on your requirements take a look at the other ComponentName constructors.

0
2

Make sure your searchables.xml file is in the correct location (i.e, in your res/xml/ folder) and that the same file does not contain any errors - otherwise you will find that (SearchManager)getSystemService(Context.SEARCH_SERVICE).getSearchableInfo(componentName) will return null, breaking your search functionality.

(Also, ensure you set componentName in the appropriate manner, because the example shown in the Android guide is for only when you are making searches and displaying searches in the same Activity.)

0

i was facing same issue adding search intent filter like this to searchable activity:

   <activity
        android:launchMode="singleTop"
        android:name=".ui.activities.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">

        <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>
-1

Double check if the corresponding entry(ie search_hint and app_label) is present in values/strings.xml

eg

      <string name="app_label">FunApp</string>
      <string name="search_hint">Caps Off</string>
1
  • he is actually not incorrect just not explaining it enough, see solution below. Sep 23, 2017 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.