summarizing search

satya - Wednesday, November 02, 2011 4:01:06 PM

Actors in a search


Search box
Suggestion Provider
Searchable Activity or Application
Search Results Activity

satya - Wednesday, November 02, 2011 4:01:58 PM

search box

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)

satya - Wednesday, November 02, 2011 4:03:15 PM

suggestion provider

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

satya - Wednesday, November 02, 2011 4:06:24 PM

Search Invoker Activity or Search Activity or Application

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

satya - Wednesday, November 02, 2011 4:07:26 PM

search results activity

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.

satya - Wednesday, November 02, 2011 4:09:32 PM

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

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

satya - Wednesday, November 02, 2011 4:13:58 PM

behavior of search for a search agnostic activity

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

satya - Wednesday, November 02, 2011 4:14:39 PM

example


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

satya - Wednesday, November 02, 2011 4:16:07 PM

You can disable search for an activity


public boolean onSearchRequested() 
{
     return false;
}

satya - Wednesday, November 02, 2011 4:17:27 PM

invoking a search through a menu


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

satya - Wednesday, November 02, 2011 4:18:30 PM

Inputs to startSearch

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.

satya - Wednesday, November 02, 2011 4:23:42 PM

The behavior of the local QSB is controlled by search meta data xml

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.

satya - Wednesday, November 02, 2011 4:26:09 PM

Here is how a search results activity is defined


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

satya - Wednesday, November 02, 2011 4:27:06 PM

The java code for the activity itself can be anything

The java code for the activity itself can be anything

satya - Wednesday, November 02, 2011 4:27:38 PM

here is the searchable.xml


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

satya - Wednesday, November 02, 2011 4:30:49 PM

details


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

satya - Wednesday, November 02, 2011 4:31:19 PM

More attributes of this xml are here

More attributes of this xml are here

satya - Wednesday, November 02, 2011 4:32:18 PM

Here is how an activity can target the searchresultsactivity as a target


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

satya - Wednesday, November 02, 2011 4:33:24 PM

A local suggestion provider is optional

You can see that teh searchable xml has omitted this

satya - Wednesday, November 02, 2011 10:27:18 PM

Explain to me Google Search in one sentence

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.

satya - Wednesday, November 02, 2011 10:30:01 PM

An activity becomes a search results activity

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.

satya - Wednesday, November 02, 2011 10:30:31 PM

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

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

satya - Wednesday, November 02, 2011 10:30:58 PM

However a suggestion provider must have an associated search results activity

However a suggestion provider must have an associated search results activity

satya - Wednesday, November 02, 2011 10:46:02 PM

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

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

satya - Wednesday, November 02, 2011 10:47:14 PM

a search dialog what we had before and searchview is new

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

satya - Wednesday, November 02, 2011 10:54:44 PM

Do you need to designate the search results activity?

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

satya - Wednesday, November 02, 2011 10:57:06 PM

SearchView is introdueced in 3.0

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

satya - Wednesday, November 02, 2011 11:00:04 PM

Ok, the clarification, the linkage

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.

satya - Wednesday, November 02, 2011 11:06:32 PM

Ok back to the search view


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

satya - Wednesday, November 02, 2011 11:08:55 PM

get search manager


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

satya - Wednesday, November 02, 2011 11:09:37 PM

locate search view in the menu


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

satya - Wednesday, November 02, 2011 11:10:43 PM

initializing the action bar searchview


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

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

satya - Wednesday, November 02, 2011 11:18:28 PM

Here is how you can add a searchview as a menu item for an action bar


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

satya - Friday, November 04, 2011 11:37:53 AM

In your searchable xml the string values cannot be literal


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

satya - Friday, November 04, 2011 1:18:41 PM

here is how you get the intent in a search results activity


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

satya - Friday, November 04, 2011 1:24:41 PM

here is how you get the query string


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

satya - Friday, November 04, 2011 1:25:57 PM

You need to do this in


@Override
public void onCreate(Bundle savedInstanceState) 
{}

and 

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

satya - Friday, November 04, 2011 1:30:18 PM

what else can i do witha search view:class ref

what else can i do witha search view:class ref

satya - Tue Nov 27 2012 12:49:16 GMT-0500 (Eastern Standard Time)

Here is the search menu item in the actionbar clarified


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

satya - Tue Nov 27 2012 12:50:43 GMT-0500 (Eastern Standard Time)

Setting up the search view


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); 
       
    }

satya - Tue Nov 27 2012 12:51:57 GMT-0500 (Eastern Standard Time)

The tie up of searchinfo for a searchview is done through the search resuts activity

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

satya - Tue Nov 27 2012 12:52:35 GMT-0500 (Eastern Standard Time)

Read about actionbar here

Read about actionbar here

satya - Tue Nov 27 2012 12:55:20 GMT-0500 (Eastern Standard Time)

Here is a picture