Search Code Snippets


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.

Search for: Android api onSearchRequested()

api docs for onserchrequested


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.

Here are the arguments and api doc for startSearch


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

<!-- /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.

Here is the documentation for this searchable.xml


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.


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

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.


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


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


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.