views:

42

answers:

3

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?

A: 

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

Chris
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
A: 

Inside of your own application, you can do this via monitoring onKeyDown(), AFAIK.

Inside other applications, this is not possible except via custom firmware.

CommonsWare
`Activity.onSearchRequested()` seems like a better bet.
Christopher
can you show me in code, how I can monitor onKeyDown() for the search button?
Sheehan Alam
@Christopher: good point
CommonsWare
@Sheehan Alam: I'd try Christopher's `onSearchRequested()` first. If not, just override `onKeyDown()` and watch for `KEYCODE_SEARCH` events.
CommonsWare
onSearchRequested() works well.
Sheehan Alam
A: 

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>
AndrewKS