× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to Populate the Search Results in Android devices

In this article, we will learn the ways how to populate the search list results in the Android devices.

To give users the benefit of predictive search, your application should send a query request to the Autocomplete API as the user types characters into an input box. The best way to do this is to set up a listener for the keyup event on the input box, and then to use the value of the string in the input box as the value for the input parameter in your request. You can display the results in a variety of ways, including as a list directly below the input box or on some other element in your application’s user interface.

Note: If you find that your app is reaching the usage limits for the Autocomplete API very quickly, it may be because you are sending requests too quickly. One approach is to set a timeout for the input box so that requests are made to the API when a user pauses typing, thereby reducing the number of total requests made to the service.

What is a search list?

When a user enters a query against the search, Android starts to provide list for possible search queries. This is based on what the user has entered so far.

Search list of the sample application

Figure 1: Search list of the sample application

Types of list

For search list to work you need to add a content provider to your application and also to configure your application’s search meta data.

Android supports two list models:

  • Your list are based on queries the user made in the past
  • Your list are based on data of your application (e.g. a database)

Recent Query List

Let us see how to display recent queries as list.

Android provides the class SearchRecentListProvider which offers a complete solution for recent list where in you need to inherit from this class and to configure it within your constructor:

Listing1: Defining the class SearchRecentListProvider

Import android.content.SearchRecentListProvider; 
public class SampleRecentListProvider 
     extends SearchRecentListProvider { 

  public static final String AUTHORITY = 
  public static final int MODE = DATABASE_MODE_QUERIES; 
   public SampleRecentListProvider() { 
      setupList(AUTHORITY, MODE); 

Now your content provider is able to return all recent queries. The thing we have done so far is to configure your content provider to provide list however you also have to save the queries so that Android can display them when the user starts another search. This is done from within your search activity when it reacts to the query:

Listing2: Returning all the search results

SearchRecentList list = 
 new SearchRecentList(this, 
list.saveRecentQuery(query, null);

As you can see SearchRecentListProvider‘s saveRecentQuery method does that for you.

Next you have to add your SearchRecentListProvider subclass as a content provider within the AndroidManifest.xml file. This is not different from the configuration of any other content provider.

Listing3: Adding the SearchRecentListProvider subclass

   android:name=".SampleRecentListProvider" >

Finally you need to add two lines to your search configuration file to enable your recent list provider:

Listing4: Code to enable recent list provider

Android:searchSuggestAuthority = 
android:searchSuggestSelection = " ?"

The database containing your application recent list

The SearchRecentListProvider stores the search history in a database calledlist.db within your application’s databases directory. This db stores an id, the query, a display text and the timestamp of the time the query was made.

The timestamp is needed since lists are sorted by time so that the most recent queries show up first. You should also consider clearing the search history from within your application – maybe even offering the user the possibility to do so. Clearing the history is also easily done. You simply have to call the method clearHistory() of the SearchRecentList object that you used above to save the search terms.

However in case you do not clear the history, Android will eventually do so itself. At most up to 250 search terms are kept in the database. Applicationlication specific list

For many applications you want to customize what list to display. Most often it’s not interesting what the user has already searched for, but what you can actually provide. For this you need to implement a content provider accessing an application-specific data source.

For search to work only the query() andgetContentType() methods are of interest. Android’s search framework calls your query() method to find out about the list of your application. But to supply list you first have to know what the user has typed into the search box so far. You get this information in one of two ways:

  • The URI contains the query as its last segment. This is the default
  • The selection parameter contains the query. The query as part of the URI

The URI Android consists of the authority of your content provider plus an optional path element you can configure in the search configuration. But in addition to these two elements Android always adds another constant to the path to make it unique for search. And as the last element Android adds the query text – the text the user has entered so far.

The constant added is search_suggest_query, defined as the final field SUGGEST_URI_PATH_QUERY within the class SearchManager.

So the URI looks like this:


The next code snippet shows what your code would look like if your content provider is not used for anything else than search:

Listing5: Code displaying search only

public Cursor query(Uri uri, String[] projection, String selection, 
    String[] selectionArgs, String sortOrder) { 
  String query = uri.getLastPathSegment(); 
  if (SearchManager.SUGGEST_URI_PATH_QUERY.equals(query)) { 
     // user hasn't entered anything 
     // thus return a default cursor
  else { 
     // query contains the users search
      // return a cursor with applicationropriate data

The query as part of a configured where clause: The other possibility to get the query is to define a where clause in your search configuration. You have to add the attribute android:searchSuggestSelection. For the sample application we use the following configuration:

android:searchSuggestSelection="name like ?"

The resulting code for a search only content provider looks very simple:

Listing6: Resulting code for a search only content provider

Public Cursor query(Uri uri, String[] projection, String selection, 
   String[] selectionArgs, String sortOrder) { 
   if (selectionArgs != null && selectionArgs.length > 0 && selectionArgs[0].length() > 0) { 
      // the entered text can be found in selectionArgs[0] 
      // return a cursor with applicationropriate data
   else { 
     // user hasn't entered anything 
     // thus return a default cursor

List threshold

By default Android queries your search list provider after any change to the text of the search box.

If your data source contains many data and you are unlikely to provide meaningful list for few characters it might be best to show list only when more characters have been typed. You can change the threshold value to achieve this. A value of “3″ would mean that Android presents list only when the user has entered at least three characters:


The cursor you return at the end of the query() method must follow strict conventions. The framework expects columns with specific names – some of which you can also configure alternatively in your configuration file. Android uses the returned values to display the list and also to call your search activity with an intent object that contains all relevant information for you to react to a selected list.

Configuration options for list clicks

To create the intent for your search activity, Android needs to know which action to use and which URI to use as value for the intent-data. You can configure both in the search configuration file using the attributes android:searchSuggestIntentAction andandroid:searchSuggestIntentData respectively.

Listing7: Using the attribute android:searchSuggestIntentAction

android:searchSuggestIntentAction = 
android:searchSuggestIntentData = 

Reacting to a list click

When a user selects a search list, Android’s search framework calls the search activity that you configured in your manifest file. It uses an explicit intent to do so. In case of an application-specific list provider this intent contains data from your configuration file and you’re returned cursor.

How to proceed depends on whether you use a recent list or an application-specific list provider. If you used a recent list provider you have to start the search for this term again to present a list of results. The action for the intent is Intent.ACTION_SEARCH.

With an application-specific list provider the list list most often shows concrete data. If a user clicks on one of these lists, he does not want to see a list of results again – instead he wants to see a detail view for the item that he selected. You can configure which action to use, but most often it will be Intent.ACTION_VIEW.

Since your search activity probably inherits from ListActivity it is not the appropriate activity for a detail view. In this case your search activity must read the id and other information contained within the intent and starts a new activity for the detail view. That’s the reason your cursor needs the data id as described above. The next code shows how to start the details activity.

Listing8: Code to define the start of the details activity

private void handleIntent(Intent intent) { 
  if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String query = intent.getStringExtra(SearchManager.QUERY); 
  } else if (Intent.ACTION_VIEW.equals(intent.getAction())) { 
     Uri detailUri = intent.getData(); 
     String id = detailUri.getLastPathSegment(); 
     Intent detailsIntent = new Intent(getApplicationlicationContext(), DetailsActivity.class); 
     detailsIntent.putExtra("ID", id); 

Global search

Basically all you have to do is to add one line to your search configuration file:


There is a catch where in your search provider only gets used when the user adds it to the list of searchable items for the Quick Search Box. The problem is not so much with the code but with how you get your user to add your application to this list:

List of globally searchable items

Figure 2: List of globally searchable items

Your application itself is not allowed to change this value – only the user can. But luckily Android provides you with an intent you can use to directly jump to the configuration page for globally searchable items.


You should consider offering your user a way to trigger this intent from within your application.

When the user has added your application to the list of searchable items, your provider’s results are shown within the search results. As you can see on the screenshot Android displays concerts of the sample application within the global list result.

Global search list showing results of the sample application

Figure3: Global search list showing results of the sample application

Problem with search list

The biggest problem with the search list is that one do not have any control over how they are going to be displayed which can cause a problem if you have customized the appearance of your application a lot and applied a custom style. In this case the list displayed by Android would most likely ruin the consistency within your application.

It gets worse if you want to present distinct data. Search in a concert application might yield results for locations as well as for bands. You might want to separate both – maybe even add section headers. You are out of luck with the standard list. You need to roll out your own list solution in this case.


As we see that Android provides us with a powerful search framework that you can control to your application’s requirement. You control whether your applications content is searchable only locally or it is searched globally. Hope you liked the tutorial.

I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login