I am not using a SearchManager, but I have built my own custom search activity. I want this to be shown when the user clicks on the Search button. How can I do this?
-
1possible duplicate of [android start user defined activity on search button pressed @ handset](http://stackoverflow.com/questions/1912947/android-start-user-defined-activity-on-search-button-pressed-handset) – Christopher Orr Sep 24 '10 at 20:11
3 Answers
If I understand you correctly, you are asking how to launch your search activity. Create an onClickListener for your button and an EditText field for the user to input text. Query the intent in your search activity to get whatever the user was searching for when the user clicks on the "search" button.
EditText et = new EditText(this);
public void onClick(View v){
String searchText = et.getText();
Intent intent = new Intent(this, com.example.app.SearchActivity.class);
intent.putExtra("searchQuery", searchText);
startActivity(intent);
}
http://developer.android.com/guide/appendix/faq/commontasks.html#opennewscreen

- 94
- 3
-
I know how to start my activity, but I would like it to be tied to the Search Button on all phones. So when you click on the magnifying glass button, it will call my activity. – Sheehan Alam Sep 24 '10 at 19:52
Inside of your own application, you can do this via monitoring onKeyDown()
, AFAIK.
Inside other applications, this is not possible except via custom firmware.

- 986,068
- 189
- 2,389
- 2,491
-
5
-
can you show me in code, how I can monitor onKeyDown() for the search button? – Sheehan Alam Sep 24 '10 at 20:47
-
@Sheehan Alam: I'd try Christopher's `onSearchRequested()` first. If not, just override `onKeyDown()` and watch for `KEYCODE_SEARCH` events. – CommonsWare Sep 25 '10 at 00:32
Not sure if this will work or not, but have you tried extending a BroadcastReceiver that can catch the search's intent? Checking the developer reference the intent seems to be "android.search.action.GLOBAL_SEARCH". So, you'll have a Receiver class like:
public class MyIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.search.action.GLOBAL_SEARCH")) {
Intent sendIntent = new Intent(context, MySearchActivity.class)
context.startActivity(intent);
}
}
}
In your manifest, between the application tags, you should have
<receiver android:name="MyIntentReceiver">
<intent-filter>
<action android:name="android.search.action.GLOBAL_SEARCH" />
</intent-filter>
</receiver>

- 3,603
- 2
- 24
- 33