Recipe Utilizing the SEARCH

If an activity in the in-focus application is defined to be searchable, the SEARCH key invokes it. A menu choice or equivalent should always be a redundant way to call the searchable activity to accommodate devices without a SEARCH key. The menu choice simply needs a call to onSearchRequested().

The searchable activity ideally should be declared as singleTop launch mode, as discussed in Chapter 2,"Application Basics:Activities and Intents."This enables multiple searches to take place without clogging the stack with multiple instances of the activity. The manifest file would have the following lines:

<activity android:name=".SearchDialogExample" android:launchMode="singleTop" > <intent-filter>

<action android:name="android.intent.action.SEARCH" /> </intent-filter>

<meta-data android:name="android.app.searchable" android:resource="@xml/my_search"/>

</activity>

Here, the XML file referencing the detail is shown in Listing 5.6.

Listing 5.6 res/xml/my_search.xml

<?xml version="1.0" encoding="utf-8"?>

<searchable xmlns:android="http://schemas.android.com/apk/res/android"

android:label="@string/app_name" android:hint="Search MyExample Here" > </searchable>

This recipe provides a search interface.When the application starts, the simplest main activity is shown in Listing 5.7 with a default main.xml file.

Listing 5.7 src/com/cookbook/search_diag/MainActivity.java package com.cookbook.search_diag;

import android.app.Activity; import android.os.Bundle;

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

Then, if the SEARCH key is selected, the searchable activity is activated.The onCreate() method checks for whether the intent is an ACTION_SEARCH, and if it is, it acts on it. Listing 5.8 shows the main activity, which just displays the query to the screen.

Listing 5.8 src/com/cookbook/search_diag/SearchDialogExample.java package com.cookbook.search_diag;

import android.app.Activity; import android.app.SearchManager; import android.content.Intent; import android.os.Bundle; import android.widget.Toast;

public class SearchDialogExample extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent intent = getIntent();

if (Intent.ACTION_SEARCH.equals(intent.getAction())) {

String query = intent.getStringExtra(SearchManager.QUERY); Toast.makeText(this, "The QUERY: " + query,

Toast.LENGTH_LONG).show();

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment