Search Code Snippets

satya - Sun Dec 02 2012 11:01:43 GMT-0500 (Eastern Standard Time)

Listing 10?1. Disabling Search Programmatically


public class NoSearchActivity extends Activity 
{
   ?..other code
   @Override
    public boolean onSearchRequested() 
    {
       return false;
    }
}

This is the callback method on the activity that is called by the search key press. here we override it and do nothing. Also return false to indicate we haven't spawned a search dialog. the base class returns false.

satya - Sun Dec 02 2012 11:02:09 GMT-0500 (Eastern Standard Time)

Android api onSearchRequested()

Search for: Android api onSearchRequested()

satya - Sun Dec 02 2012 11:02:54 GMT-0500 (Eastern Standard Time)

api docs for onserchrequested

api docs for onserchrequested

satya - Sun Dec 02 2012 11:06:36 GMT-0500 (Eastern Standard Time)

Listing 10?2. Invoking Search through a Menu


public class SearchInvokerActivity extends Activity 
{
    ?..other stuff    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
       if (item.getItemId() == R.id.mid_si_search)
       {
          this.onSearchRequested();
          return true;
       }
       return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onSearchRequested() 
    {
        this.startSearch("test",true,null,true);
        return true;
    }
}

This code explicitly start the global search by properly calling the startSearch method. The base class implementation is not invoking the global search. It does invoke a local search if available.

satya - Sun Dec 02 2012 11:07:33 GMT-0500 (Eastern Standard Time)

Here are the arguments and api doc for startSearch

Here are the arguments and api doc for startSearch

satya - Sun Dec 02 2012 11:09:34 GMT-0500 (Eastern Standard Time)

Listing 10?5. Defining a Search Results Activity in the manifest file with search meta data


<activity android:name=".SearchActivity"
   android:label="Activity/QSB Interaction::Search Results">
     <intent-filter>
        <action android:name="android.intent.action.SEARCH"/>
        <category android:name="android.intent.category.DEFAULT"/>
      </intent-filter>

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

</activity>

satya - Sun Dec 02 2012 11:10:30 GMT-0500 (Eastern Standard Time)

Listing 10?6. SearchableInfo for a search results activity


<!-- /res/xml/searchable.xml -->
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/search_label"
    android:hint="@string/search_hint" 
    android:searchMode="showSearchLabelAsBadge"
/>

This xml file will be in the xml sub directory.

satya - Sun Dec 02 2012 11:10:59 GMT-0500 (Eastern Standard Time)

Here is the documentation for this searchable.xml

Here is the documentation for this searchable.xml

satya - Sun Dec 02 2012 11:15:47 GMT-0500 (Eastern Standard Time)

Listing 10?7. LocalSearchEnabledActivity: How to respond to local search properly


public class LocalSearchEnabledActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.local_search_enabled_activity);
        return;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)     {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater(); 
        inflater.inflate(R.menu.search_invoker_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)     {
        if (item.getItemId() == R.id.mid_si_search)
        {
            onSearchRequested();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

This menu pattern is important to cover for all cases of search key, search view with an accompanying menu item etc. Bottom line is you have to respond to the menu item as well as a last resort.

satya - Sun Dec 02 2012 11:16:50 GMT-0500 (Eastern Standard Time)

Listing 10?8. Tieing a Search Results Activity through metadata to a search invoking activity


<activity android:name=".LocalSearchEnabledActivity"
   android:label="Activity/QSB Interaction::Local Search">
     <meta-data android:name="android.app.default_searchable"
         android:value=".SearchActivity" />
</activity>

This is how you tell your application or activities that they have a corresponding search activity.

satya - Sun Dec 02 2012 11:19:09 GMT-0500 (Eastern Standard Time)

Note

Note: You can this meta data definition at the application level as well so that all activities will inherit this search activity. Individual activities can further override the application level search activity if needed. Prior releases used to accept a "*" in this place to indicate a global search. This "*" specification is deprecated now.

satya - Sun Dec 02 2012 11:20:48 GMT-0500 (Eastern Standard Time)

Here is how you enable type to search for an activity


this.setDefaultKeyMode(Activity.DEFAULT_KEYS_SEARCH_GLOBAL);
or
this.setDefaultKeyMode(Activity.DEFAULT_KEYS_SEARCH_LOCAL);

Very curiously this works for global search as well while the search key will not invoke the global search by default!!

satya - Sun Dec 02 2012 11:22:00 GMT-0500 (Eastern Standard Time)

Listing 10-9. Search View Menu Item Definition


<item android:id="@+id/menu_search"
    android:title="Search"
    android:showAsAction="ifRoom"  
    android:actionViewClass="android.widget.SearchView"            
    />

This is how you present a search view in the action bar. this will show up the view in the actionbar. However you need to write some java code to attach the searchable.xml file to the search view.

satya - Sun Dec 02 2012 11:23:22 GMT-0500 (Eastern Standard Time)

Listing 10-10. Tieing search view widget to the search results activity


private void setupSearchView(Menu menu)
{
    //Locate the search view widget
    SearchView searchView = 
        (SearchView) menu.findItem(R.id.menu_search).getActionView();
    if (searchView == null)
    {
        Log.d(tag, "Failed to get search view");
        return;
    }

    //setup searchview
    SearchManager searchManager = 
        (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    ComponentName cn = 
        new ComponentName(this,SearchActivity.class);
    SearchableInfo info = 
        searchManager.getSearchableInfo(cn);
    if (info == null)
    {
        Log.d(tag, "Failed to get search info");
        return;
    }

    searchView.setSearchableInfo(info);

    // Do not iconify the widget; expand it by default
    searchView.setIconifiedByDefault(false); 
}

Notice that the attachment of the searchable xml file to the searchview is indirect. It is done by attaching the search results activity to the search view. The results activity is the one that has the direct linkage to the searchableinfo xml file.