The android platform provides us with a search framework that helps us implementing search mechanism in our application. We can allow the user to enter the searched term either through a search dialogue shown on top of the screen or through a search view widget.

When developing an application that includes a searching mechanism we should configure that searching using an xml file. The name of this file is usually searchable.xml and you should save it within the rex\xml folder.

We should develop an activity that will function as a searchable activity. This is the activity that receives the search query, searches the application data, and displays the search results. When the user executes a search in the search dialog or widget, the system starts the searchable activity and send it the search query. The search query is bundled within the intent the system created in order to start the searchable activity. The action of that intent should be ACTION_SEARCH.

The searchable activity should be defined in the manifest file together with a meta data that sets it to function as a searchable activity that performs the search and presents the results. Other activitis should be defined in the manifest file togethe with another meta data that enables the search dialogue. The code sample I created includes two activities. MainActivity is a simple activity that includes a button on which the user can press in order to pop up the search dialogue. SearchActivity is the activity that will be invoked when the user enters the searched query and presses enter. SearchActivity will get the searched query together bundled within the explicit intent that invokes it.

Hereto the manifest file that defines these two activities.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lifemichael.searchsample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchActivity" />

        <activity
            android:name="com.lifemichael.searchsample.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.lifemichael.searchsample.SearchActivity"
            android:label="@string/title_activity_search" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
    </application>
</manifest>

Hereto the source code of the MainActivity.

package com.lifemichael.searchsample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button bt = (Button) findViewById(R.id.btSearch);
		bt.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				onSearchRequested ();
			}
		});
	}

}

Hereto the source code of the XML document that defines the layout of the MainActivity.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="74dp"
        android:text="@string/search_text" />

</RelativeLayout>

Hereto the source code of SearchActivity that receives the searched query from the MainActivity, performs the search and displays the result.

package com.lifemichael.searchsample;

import java.util.Hashtable;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class SearchActivity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		Map<String,String> map = new Hashtable<String,String>();
		map.put("Shalom", "Hello in Hebrew");
		map.put("Salam","Hello in Arabic");
		map.put("Bonjour", "Hello in French");
		map.put("Hola","Hello in Spanish");
		String result = "found nothing!";
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		if (Intent.ACTION_SEARCH.equals(intent.getAction()))
		{
			String query = intent.getStringExtra(SearchManager.QUERY);
			Log.i("search","query="+query);
			String str = map.get(query);
			if(str!=null)
			{
				result = "result: "+query+" is "+str;
			}
		}
		TextView text = new TextView(this);
		text.setText(result);
		setContentView(text);
	}
}

The following video clip shows the execution of this code sample, overviews it and explains each and every part of it.

You can find a detailed presentation that exlpains the Android Framework in my ‘Android Fundamentals’ community course. You can find it at http://abelski.lifemichael.com. Detailed specification and explanations can be found at http://developer.android.com/guide/topics/search/index.html.

Leave a Reply

Your email address will not be published. Required fields are marked *