Creating a Search Activity

To enable application search, you must create an Activity that will be used to initiate and display the search.

The first step is to create a new searchable metadata XML resource in the res/xml folder. This file, shown in Listing 10-18, specifies the authority of the Content Provider you will be performing the search on, and the action to fire if a suggested search result is clicked.

STING 10-18: Defining application search metadata

<searchable xmlns:android="" android:label="@string/app_name" android:searchSuggestAuthority="myauthority"

android:searchSuggestIntentAction="android.intent.action.VIEW"> </searchable>

Next, you will need to create an Activity that will be used to display the search results. In many cases this will be a simple List View-based Activity, but it can use any user interface you require. As shown in Listing 10-19, include a <meta-data> tag that includes a name attribute that specifies and a resource attribute that specifies the XML resource you created in Listing 10-18.

You must also include an Intent Filter registered for the android.intent.action.SEARCH action and the default category.

LISTING 10-19: Registering a search results Activity

<activity android:name=".EarthquakeSearch" android:label="Earthquake Search"> <intent-filter>

<action android:name="android.intent.action.SEARCH" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <meta-data android:name="" android:resource="@xml/searchable"


The search query that caused this search result Activity to be displayed will be returned within the calling Intent using the SearchMananger.USER_QUERY extra as shown in the following:

String searchTerm = getIntent().getStringExtra(SearchManager.USER_QUERY);

It's good practice to use the same search results form for your entire application. To set an Activity as the default search results provider for an application you need to add a new <meta-data> tag to the <application> manifest node as shown in Listing 10-20.

Set the name attribute to and specify your search Activity using the value attribute.

Available for download on

LISTING 10-20: Setting a default search result Activity for an application Available for download on <meta-data android:name=""


Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment