summarizing search


Search box
Suggestion Provider
Searchable Activity or Application
Search Results Activity

This is sometimes also called a QSB. quick search box. Same box is used to search inside your own application (local search) and also at the device/widget level (global search)

A registerable java class that can receive and respond with search suggestions to what is being typed in the search. This has an XML file that can tailor its behavior

An activity that indicates that it wants to specialize its search. This activity points to a custom activity that can respond to search strings.

This activity receives the search words that are entered in the qsb or extracted from the suggestions and passed. This activity then use those strings to perform the search and produce results.

In 2.3 and later global search will allow you to pick a particular suggestion provider

As of 2.3 global search is not invoked. You can enable this if you want by overriding onSearchRequested() method


@Override
public boolean onSearchRequested() 
{
  Log.d(tag,"onsearch request called");
  this.startSearch("test",true,null,true);
  return true;
}

public boolean onSearchRequested() 
{
     return false;
}

private void invokeSearch()
{
   this.onSearchRequested();
}
@Override
public boolean onSearchRequested() 
{
    this.startSearch("test",true,null,true);
    return true;
}

initialQuery: Text to search for.

selectInitialQuery: A boolean indicating whether to highlight the search text or not. In this case we used ?true? to hightlight the text so that it can deleted in favor of a new text if desired.

appSearchData: A bundle object to pass to the search activity. In our case, we are not targeting any particular search activity we passed null.

globalSearch: If it is true, the global search is invoked. If it is false, a local search is invoked if available; otherwise a global search is invoked.

This is defined through the search results activity and its corresponding meta data xml for search. You will get to customize such things as title, default search pattern, suggestion provider etc.


<activity android:name=".SearchResultsActivity"
   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>

The java code for the activity itself can be anything


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

label: label of the search box
hint: default hint in the search box

More attributes of this xml are here


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

You can see that teh searchable xml has omitted this

if an android application contains an activity that knows how to receive a search string and display the results then that application can present a search box to respond to.

by indicating that it can respond to a search intent and that it has an associated xml file that describes how to customize the search experience.

You can have a search results activity with out a suggestion provider

However a suggestion provider must have an associated search results activity

Search Dialog and Search View widget introduced in 3.0: Read about these

Both are designed to work similar. However a searchview is designed to give you full control if needed. the rest of the concepts seem to stay the same

the older docs clearly suggest that either your app or another activity must declare the target search results activity

However the recent docs doesn't seem to indicate this relationship is necessary. It seem to indicate that the mere mention of a search action intent filter seem to do the trick. Not sure. need to check

the recommendation seem to be to use the SearchView widget as part of the action bar as opposed to the search dialog.

If you want to use the search dialog approach, then the activity that wants the dialog must declare its target search results activity. This is the old behavior. And it seem to hold still.


1. tell actionbar we need a searchview menu
2. locate the searchview from the menu
3. set the searchable xml file on the searchview

SearchManager searchManager = 
(SearchManager) getSystemService(Context.SEARCH_SERVICE);

SearchView searchView = 
(SearchView) menu.findItem(R.id.menu_search).getActionView();

searchView.setSearchableInfo(
  searchManager.getSearchableInfo(getComponentName()));    

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

android:showAsAction="ifRoom"  
android:actionViewClass="android.widget.SearchView"

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

These cannot be as below


<!-- /res/xml/searchable.xml -->
<searchable xmlns:android="http://schemas.android.com/apk/res/android"

    android:label="search_label"
    android:hint="search_hint" 

    android:searchMode="showSearchLabelAsBadge"
/>

//Get the intent from the activity
final Intent queryIntent = getIntent();

//see its action
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) 
{
   //do somethign with the intent
   //such as see the query string
}
else
{
  //iam invoked differently
}

final String queryString = 
       queryIntent.getStringExtra(SearchManager.QUERY);

@Override
public void onCreate(Bundle savedInstanceState) 
{}

and 

@Override
public void onNewIntent(final Intent newIntent) 
{}

what else can i do witha search view:class ref


<menu 
xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This group uses the default category. -->
    <group android:id="@+id/menuGroup_Main">

        <item android:id="@+id/menu_search"
            android:title="Search"

         android:showAsAction="ifRoom"  
         android:actionViewClass="android.widget.SearchView"            

            />
        <item android:id="@+id/menu_action_icon1"
            android:title="Action Icon1" 
            android:icon="@drawable/creep001" 
            android:showAsAction="ifRoom"/>

private void setupSearchView(Menu menu)
    {
       SearchView searchView = 
          (SearchView) menu.findItem(R.id.menu_search).getActionView();
       if (searchView == null)
       {
          this.reportBack(tag, "Failed to get search view");
          return;
       }
       //setup searchview
       SearchManager searchManager = 
          (SearchManager) getSystemService(Context.SEARCH_SERVICE);
       ComponentName cn = 
          new ComponentName(this,SearchResultsActivity.class);
       SearchableInfo info = 
          searchManager.getSearchableInfo(cn);
       if (info == null)
       {
          this.reportBack(tag, "Failed to get search info");
          return;
       }

       searchView.setSearchableInfo(info);

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

Notice that in the code above the searchableinfo is obtained by indicating the name of the search results activity class.

Read about actionbar here