Thursday, December 1, 2011

Implement Search interface

In this exercise, we will implement search interface on our activity, without actual searching function. When user press the Search button, a search dialog will be opened; then after user confirm search, the query will be passed to another activity (MySearch.java) for searching.

Implement Search interface

First we create our search activity, MySearch.java. Simple check if the intent equal to ACTION_SEARCH and display the query only.
package com.exercise.AndroidSearch;

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

public class MySearch extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Toast.makeText(MySearch.this,
query,
Toast.LENGTH_LONG).show();
}
}

}



Modify AndroidManifest.xml to add MySearch activity to handle intent of "android.intent.action.SEARCH", with meta-data with android:name="android.app.searchable" and android:resource="@xml/searchable". xml/searchable.xml is the searchable configuration, it will be created later.
Also add meta-data in the main activity AndroidSearchActivity, with android:name="android.app.default_searchable" and android:value=".MySearch". Where MySearch.java is the search activity we have just created.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidSearch"
android:versionCode="1"
android:versionName="1.0" >

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

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AndroidSearchActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".MySearch" />
</activity>
<activity android:name=".MySearch" >
<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>


Create /res/xml/searchable.xml to define our searchable configuration.
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name" >
</searchable>


The main activity have no need to change in this exercise.

Download the files.



2 comments:

john said...

nice tutorial, thanks so much.

Can you tell me to change button icon and
when i click on button it show quickaction dialog (choice search type).

thanks so much :)

krishnan kumar said...

Thank you so much for the tutorial... Appreciate that! Now if I want to maintain the search with respect to a specific site or url.. How will I do that? For example. I want to show all videos from YouTube when the user enters a search query in the search box. How will I be able to do this? Your help will be useful to many people.. Thanks in advance.