How to Create a Search Interface in Android
Today Learn How to Create a Search Interface in Android
1.MainActivity.java:
import android.content.Intent;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
public class MainActivty extends Search {
@Override
ListAdapter makeMeAnAdapter(Intent intent) {
return(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items));
}
}
2.Search.java:
import android.os.Bundle;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
abstract public class Search extends ListActivity {
abstract ListAdapter makeMeAnAdapter(Intent intent);
private static final int LOCAL_SEARCH_ID = Menu.FIRST + 1;
private static final int GLOBAL_SEARCH_ID = Menu.FIRST + 2;
EditText selection;
ArrayList<String> items = new ArrayList<String>();
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
selection = (EditText) findViewById(R.id.selection);
try {
XmlPullParser xpp = getResources().getXml(R.xml.words);
while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
if (xpp.getEventType() == XmlPullParser.START_TAG) {
if (xpp.getName().equals("word")) {
items.add(xpp.getAttributeValue(0));
}
}
xpp.next();
}
} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(), 4000)
.show();
}
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
onNewIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
ListAdapter adapter = makeMeAnAdapter(intent);
if (adapter == null) {
finish();
} else {
setListAdapter(adapter);
}
}
public void onListItemClick(ListView parent, View v, int position, long id) {
selection.setText(parent.getAdapter().getItem(position).toString());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, LOCAL_SEARCH_ID, Menu.NONE, "Local Search")
.setIcon(android.R.drawable.ic_search_category_default);
menu.add(Menu.NONE, GLOBAL_SEARCH_ID, Menu.NONE, "Global Search")
.setIcon(R.drawable.search)
.setAlphabeticShortcut(SearchManager.MENU_KEY);
return (super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case LOCAL_SEARCH_ID:
onSearchRequested();
return (true);
case GLOBAL_SEARCH_ID:
startSearch(null, false, null, true);
return (true);
}
return (super.onOptionsItemSelected(item));
}
}
3.SearchInterface:
import java.util.ArrayList;
import java.util.List;
import android.app.SearchManager;
import android.content.Intent;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
public class SearchInterface extends Search {
@Override
ListAdapter makeMeAnAdapter(Intent intent) {
ListAdapter adapter=null;
if (intent.getAction().equals(Intent.ACTION_SEARCH)) {
String query=intent.getStringExtra(SearchManager.QUERY);
List<String> results=searchItems(query);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,results);
setTitle("Search : "+query);
}
return(adapter);
}
private List<String> searchItems(String query) {
SearchSuggestionProvider
.getBridge(this)
.saveRecentQuery(query, null);
List<String> results=new ArrayList<String>();
for (String item : items) {
if (item.indexOf(query)>-1) {
results.add(item);
}
}
return(results);
}
}
4.activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="60dp" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" />
</LinearLayout>
5.Word.xml:
<words>
<word value="C" />
<word value="gcc" />
<word value="C++" />
<word value="codeBlocks" />
<word value="Java" />
<word value="NetBeans" />
<word value="Eclipse" />
<word value="Android" />
<word value="Eclipse" />
<word value="PHP" />
<word value="MIPS" />
<word value="SQL" />
<word value="SQLite" />
<word value="XML" />
<word value="C#" />
<word value="Adodbe" />
<word value="MATLAB" />
<word value=".NET" />
<word value="Google" />
</words>
6.Manifest.xml add code:
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
No comments:
Post a Comment