0
votes

I am trying to implement the SearchView in my ToolBar, but when I type something and confirm the search, it does not open up the activity for result.

The search appears in MainActivity and when the user types something and presses the search button, it should open SearchResultAcivity. But it does not open.

Here is what I have done:

AndroidManifest:

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

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

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

</activity>

<activity
    android:name=".SearchResultsActivity">

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

</activity>

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="search" >
</searchable>

Menu Item:

<item
    android:id="@+id/action_search"
    android:title="Search"
    android:orderInCategory="100"
    android:icon="@drawable/search"
    app:actionViewClass = "android.support.v7.widget.SearchView"
    app:showAsAction="collapseActionView|ifRoom" />

And:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(true); // Iconify the widget


return true;
}
1

1 Answers

1
votes

In your manifest you make a mistake.

I assume that you want search in all activitys and for working you need to put this before mainactivity:

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

And put this inside searchresultactivity:

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

the result is like that:

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

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

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