This took a bit for me to figure out, but posting here as its probably the quickest tute I could think of for the new Android board.
Ive attached an image of what the wiki one looks like.
1) In your .Main Activity you need to set the OptionsMenu (if you want to trigger it from that) so add the 'onCreateOptionsMenu' and 'onOptionsItemSelected' methods.
The 'onSearchRequested()' method is the important part that triggers the search UI.
private static final int SEARCH = Menu.FIRST;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, SEARCH, SEARCH, "Search").setIcon(R.drawable.ic_search_icon);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case SEARCH:
onSearchRequested();
return true;
}
return super.onOptionsItemSelected(item);
}
2) Next you want to let it know which activity will take the search result and do something with it.
So in your manifest file...
<activity android:name=".Main"
android:label="@string/app_name">
<meta-data android:name="android.app.default_searchable" android:value=".SearchFor" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchFor" android:label="@string/title_searchonline">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
</activity>
3) Now we need to get results from the search box so that we can search something with it.
On the receiving activity that was defined in your manifest...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchtabs);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//Do stuff here
}
}
There are probably some errors in here, as I copy/pasted from my own app code. But it should help anyone looking to do this get the general idea of how it works..