Adding Search to the Earthquake Example

In the following example you'll add search functionality to the Earthquake project, and make sure results are available from the home-screen Quick Search Box.

1. Start by adding two new string resources to the strings.xml file in the res/values folder. One will be the name used to identify the earthquake search results; the other will be a description of what they represent.

<string name="search_label">Earthquakes</string>

<string name="search_description">Earthquake locations</string>

Prepared for ASHLEE KABAT, email: [email protected] Order number: 56760408 This PDF is for the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit www.wrox.com to purchase your own copy.

Available for download on Wrox.com

2. Next, create a new XML resources folder, res/xml. Create a new searchable.xml file which will define the metadata for your Earthquake search results provider. Specify the strings from Step 1 as the label and description values. Specify the Earthquake Content Provider's authority and set the includelnGlobalSearch attribute to true.

<searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_name"

android:searchSettingsDescription="@string/app_name" android:includeInGlobalSearch="true"

android:searchSuggestAuthority="com.paad.provider.earthquake" android:searchSuggestIntentAction="android.intent.action.VIEW"> </searchable>

3. Open the Earthquake Content Provider. Start by adding a new search_uri static constant that you can use to execute a search within the application.

public static final Uri SEARCH_URI =

Uri.parse("content://com.paad.provider.earthquake/" + SearchManager.SUGGEST_URI_PATH_QUERY);

4. Now create a new Projection that will be used to supply search results.

private static final HashMap<String, String> SEARCH_PROJECTION_MAP; static {

SEARCH_PROJECTION_MAP = new HashMap<String, String>();

SEARCH_PROJECTION_MAP.put(SearchManager.SUGGEST_COLUMN_TEXT_1, KEY_DETAILS +

" AS " + SearchManager.SUGGEST_COLUMN_TEXT_1); SEARCH_PROJECTION_MAP.put("_id", KEY_ID + " AS " + "_id");

5. Now modify the UriMatcher to include search queries. private static int SEARCH = 3;

static {

uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

uriMatcher.addURI("com.paad.provider.earthquake", "earthquakes", QUAKES); uriMatcher.addURI("com.paad.provider.earthquake", "earthquakes/#", QUAKE_ID); uriMatcher.addURI("com.paad.provider.earthquake",

SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH); uriMatcher.addURI("com.paad.provider.earthquake",

SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH); uriMatcher.addURI("com.paad.provider.earthquake",

SearchManager.SUGGEST_URI_PATH_SHORTCUT, SEARCH); uriMatcher.addURI("com.paad.provider.earthquake",

SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*", SEARCH);

6. Modify the getType method to return the appropriate MIME type for search results. ©Override public String getType(Uri uri) { switch (uriMatcher.match(uri)) {

case QUAKES : return "vnd.android.cursor.dir/vnd.paad.earthquake"; case QUAKE_ID: return "vnd.android.cursor.item/vnd.paad.earthquake"; case SEARCH : return SearchManager.SUGGEST_MIME_TYPE;

default: throw new IllegalArgumentException("Unsupported URI: " + uri);

7. The final change to the Content Provider is to modify the query method to apply the search term and return the result query using the Projection you created in Step 4. This will allow the Quick Search Box search suggestions, and your search Activity, to display the results.

©Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sort) { SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); qb.setTables(EARTHQUAKE_TABLE);

// If this is a row query, limit the result set to the passed in row. switch (uriMatcher.match(uri)) {

case QUAKE_ID: qb.appendWhere(KEY_ID + "=" + uri.getPathSegments().get(1)); break;

case SEARCH : qb.appendWhere(KEY_DETAILS + " LIKE \"%" +

uri.getPathSegments().get(1) + "%\""); qb.setProjectionMap(SEARCH_PROJECTION_MAP); break;

default : break;

8. Now create a new Activity that will be used to display the search results. For these purposes, create a simple EarthquakeSearch Activity that extends ListActivity. EarthquakeSearch will only be displayed as a result of a search query, so extract the user query from the search Intent that launched the application and use it to query the Earthquake Content Provider. Create a Simple Cursor Adapter to bind the search results cursor to the Activity's List View.

import android.app.ListActivity; import android.app.SearchManager; import android.database.Cursor; import android.net.Uri; import android.os.Bundle;

import android.widget.SimpleCursorAdapter;

public class EarthquakeSearch extends ListActivity { ©Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

String searchTerm = getIntent().getStringExtra(SearchManager.USER_QUERY); String searchQuery = Uri.withAppendedPath(EarthquakeProvider.SEARCH_URI, searchTerm);

Cursor c = getContentResolver().query(searchQuery, null, null, null, null); startManagingCursor(c);

String[] from = new String[] {SearchManager.SUGGEST_COLUMN_TEXT_1}; int[] to = new int[] {android.R.id.textl};

SimpleCursorAdapter searchResults = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, from, to); setListAdapter(searchResults);

9. Open the application Manifest and add the new EarthquakeSearch Activity. Make sure you add an Intent Filter for the search action in the default category. You will also need to add a <meta-data> tag that specifies the searchable XML resource you created in Step 2.

<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.app.searchable" android:resource="@xml/searchable"

</activity>

10. The final step is to add a new <meta-data> tag to the <application> node in the manifest that describes the EarthquakeSearch Activity as the default search provider for the application.

<application android:icon="@drawable/icon"> <meta-data android;name="android.app.default_searchable" android;value=".EarthquakeSearch"

[ ... existing application node ... ] </application>

All code snippets in this example are part of the Chapter 10 Earthquake project, available for download at Wrox.com.

If you run this application, pressing the hardware search key in any of the Activities will provide a search box that returns suggestions and search results as you type. To have your results available in the home screen Quick Search Box, you will need to go to Settings O Search O Searchable Items and tick the "Earthquake" item.

CREATING LIVE WALLPAPER

Live Wallpaper is a new way to add an application component to the home screen introduced in Android 2.1 (API level 7). Live Wallpaper lets you create dynamic, interactive home-screen backgrounds, providing you with an exciting new alternative for displaying information to your users directly on the home screen.

Live Wallpaper uses a Surface to render a dynamic display and listens for screen touch events to let users interact with the display.

To create a new Live Wallpaper you need three components:

> A Live Wallpaper XML resource

> A Wallpaper Service implementation

> A Wallpaper Engine implementation (returned through the Wallpaper Service)

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