17

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?

Maarten
  • 265
  • 1
  • 2
  • 9
  • So I assume that you have data to search through right? Are you also implementing any interfaces in your activity? – Eenvincible Apr 09 '15 at 14:17

3 Answers3

40

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:

Community
  • 1
  • 1
chRyNaN
  • 3,592
  • 5
  • 46
  • 74
1

I understood the problem i had also faced the same, This is happening because you are passing the current component name by passing the

getComponentName()

This will be initialize by the current activity name so you need to initialize it with the searchable activity name in given below format and pass the same Component instance it starts the new activity.

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    search.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
    search.setQueryHint(getResources().getString(R.string.search_hint));

Hope it I have answered the Question!

Vivek Samele
  • 340
  • 4
  • 8
0

Without seeing your activity code, I would suggest you try this approach - also assuming you have all the files created as stated above;

In your results activity,

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

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    searchView.setIconifiedByDefault(false);
    searchView.setQueryHint(getString(R.string.search_hint));

    searchView.setOnQueryTextListener(this);

    return true;
}

Remember that this is the activity that has your data that you want to make searchable:

You must implement the SearchView.OnQueryTextListener interface in the same activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_search:
            ProductsResulstActivity.this.finish();

            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {

    productFilterAdapter.getFilter().filter(newText);

    if (TextUtils.isEmpty(newText)) {
         listView.clearTextFilter();
     }
    else {
       listView.setFilterText(newText);
    }

    return true;
}

productFilterAdapter is the adapter that you must create beforehand.

It should implement Filterable interface. I hope this helps.

If you need further assistance, please let me know. Good luck

Eenvincible
  • 5,641
  • 2
  • 27
  • 46