Search code examples
androidandroid-actionbarandroid-searchmanager

Search on action bar does not call search activity


I am trying to make a search bar and I followed the tutorial on http://developer.android.com/training/search/setup.html. It does not work and SearchResultsActivity does not get called, so I immediately found the following question: Search widget on action bar doesn't trigger my search activity. However, the solutions listed there (more specifically, adding meta-data inside in AndroidManifest.xml and using references to strings in xml/searchable.xml) do not solve my problem. I also tried putting meta-data in <activity> in AndroidManifest.xml and numerous other solutions to no avail.

What am I doing wrong?

Here is my code:

main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.jsondataparser.app.MainActivity" >
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
    <item android:id="@+id/search_menu"
        android:title="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView" />
</menu>

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/app_name">
</searchable>

AndroidManifest.xml:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity" />
        <activity
            android:name="com.jsondataparser.app.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.jsondataparser.app.SearchResultsActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
</application>

MainActivity.java:

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search_menu).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return true;
}

SearchResultsActivity.java:

public class SearchResultsActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d("SearchResultsActivity", "onCreate called");
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
        Log.d("SearchResultsActivity", "onNewIntent called");
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);

            Log.d("SearchResultsActivity", query);
        }
    }
}

Solution

  • Put also

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

    inside <activity> in your AndroidManifest.