Start new activity from SearchView

Maarten picture Maarten · Apr 9, 2015 · Viewed 13k times · Source

I have 2 activities: the first has a action bar with a search view, the second should display the results of the search query.

androidmanifest:

    <activity
        android:name=".SearchActivity"

        ...
        android:launchMode="singleTop">           

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

    </activity>

   <activity
        android:name=".ResultsActivity"
        ...

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

    </activity>

searchable.xml

<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/enter_a_word" />

SearchActivity

....
@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_noun_list, menu);

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

    return true;
}
....

ResultsActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);

    }
...
}

the problem is that after a query is entered into the searchview, nothing happens. No errors, nothing. How can i open the resultsactivity after the query is entered in the searchactivity?

Answer

chRyNaN picture chRyNaN · Aug 19, 2015

This answer is a little late but I feel it'll be useful for future viewers. The dilemma seems to come from the ambiguity of the Android SearchView tutorial. The scenario they cover assumes you will be displaying the results in the same Activity the SearchView resides. In such a scenario, the Activity tag in the AndroidManifest.xml file would look something like this:

<activity
    android:name=".MainActivity"
    android:label="@string/main_activity_label"
    android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH"/>
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />
</activity>

Then, to handle the results in the same Activity, you would Override the onNewIntent method:

@Override
public void onNewIntent(Intent intent){
    setIntent(intent);
    if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        //now you can display the results
    }  
}

However, in a situation where we want to display the results in another Activity, we must put the Intent Filter and meta tag into the results Activity and introduce a new meta tag for the SearchView Activity. So, our Activities will look something like this in the AndroidManifest.xml file:

<activity
        android:name=".MainActivity"
        android:label="@string/main_activity_label"
        android:launchMode="singleTop">
        <!-- meta tag points to the activity which displays the results -->
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchResultsActivity" />
</activity>
<activity
        android:name=".SearchResultsActivity"
        android:label="@string/results_activity_label"
        android:parentActivityName="com.example.MainActivity">
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.MainActivity" />
        <!-- meta tag and intent filter go into results activity -->
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
</activity>

Then, in our MainActivity's onCreateOptionsMenu method, activate the SearchView (assumes you're adding the SearchView to the ActionBar). Rather than using getComponentName() in the SearchManager's getSearchableInfo() method call, we instantiate a new ComponentName object using the MainActivity's context and the SearchResultsActivity class:

@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_home, menu);
    SearchView search = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search);
    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    search.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
    search.setQueryHint(getResources().getString(R.string.search_hint));
    return true;
}

Finally, in our SearchResultsActivity class, in the onCreate method, we can handle the search results:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        //use the query to search your data somehow
    }
}

Don't forget to create the searchable.xml resource file and add the SearchView to your layout.

searchable.xml (res/xml/searchable.xml; create xml folder under res if needed):

<?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/search_hint"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>

Layout (example of adding the SearchView to ActionBar as a menu item):

<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.example.MainActivity">
    <group android:checkableBehavior="single">
        <item android:id="@+id/action_search" android:title="Search"
            android:orderInCategory="1" app:showAsAction="collapseActionView|ifRoom"
            app:actionViewClass="android.support.v7.widget.SearchView"/>
    </group>
</menu>

Resources: