I’m following http://developer.android.com/guide/topics/search/search-dialog.html to create a searchable activity for my app.
SearchableActivity.java
package com.xxxx.xx;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import com.xxxx.xx;
public class SearchableActivity extends ListActivity {
protected SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// 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);
doMySearch(query);
}
}
public List<String> doMySearch(String query) {
List<String> result = new ArrayList<String>();
Cursor c = db.query(
"employee",
new String[] { "_id" }, // The column you want as a result of the Query
"firstName like '%?%' OR lastName like '%?%' OR officePhone like '%?%'", // The where-Clause of the statement with placeholders (?)
new String[] { query, query, query }, // One String for every Placeholder-? in the where-Clause
null, // if you like a order by put it here
null, // group by here
null // having here
);
while (c.moveToNext()) {
result.add(c.getString(0));
}
c.close();
return result;
}
}
/res/xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable> xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint" >
</searchable>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxx.xx"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.CALL_PHONE" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<meta-data android:name="android.app.default_searchable"
android:value="com.xxxx.xx.SearchableActivity" />
<activity android:name="com.xxxx.xx.TestList"
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.xxxx.xx.SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
<activity android:name="com.xxxx.xx.TestDetails"></activity>
<activity android:name="com.xxxx.xx.TestList">
<meta-data android:name="android.app.default_searchable"
android:value="com.xxxx.xx.SearchableActivity" />
</activity>
<activity android:name="com.xxxx.xx.DirectReports"></activity>
</application>
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14"/>
I have added all these but the device SEARCH button is not opening the search dialog. Why?
I’m not sure about this, but it might be because you declare your application’s package as
package="com.xxxxxxx.tests", but in all of your activity references you usesamples.xxxxxxxxx.SearchableActivity. Try updating your app package name to"samples.xxxxx"and see if that fixes it. Also, in your code you have your package declared aspackage com.xxxx.xx;. So, make up your mind and make all of the references consistent.