8

In the system-wide search on my HTC Desire (Froyo), I see a little drop down left to the search input field that allows to select where I want to search (All, web, apps).

How can I implement this in an application of mine? The search tutorial on the Google developer site does not address this.

So in a scenario like the following, taken from the Android docs,

I would like to click on the books and then get some sort of menu to e.g. select "words", "headings" as search mode.

Update: I am not looking for the QuickAction dialog itself, but rather how to attach something to the books icon that reacts on touch, so that I could attach the QuickAction or a new activity or ... And I want to use the standard Android Search Dialog as described in http://developer.android.com/guide/topics/search/search-dialog.html

Community
  • 1
  • 1
Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119

4 Answers4

2

It is just a custom view that get rendered when you hit that button with a fancy animation.

It has nothing to do with the search framework per se. You just show a custom layout (with a fancy animation if you want) and set a value by clicking on one of the icons. Thats it.

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
2

You can't modify the system's search dialog. So if you want customizations like making a drop-down menu appear when the user touches the icon, you'll have to implement your own dialog. This would make your search non-standard so I wouldn't advise it.

However, if I can't talk you out of it, you can see how Android's built-in search dialog is implemented here:

In Android 3.x this gets a little easier with the introduction of the SearchView class.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ed Burnette
  • 1,198
  • 8
  • 13
1

I think what your looking for is called a "quickaction dialog".

Here's a tutorial that should have you well on your way.

Updated by question asker (paste of comment):

ah i see. well somehow you need the id of the icon in order to implement the onclicklistener(). you may be stuck just going with a search widget and implementing most by scratch. Or perhaps, with the search dialog implemented and running, use hierarchyviewer to see if the icon has an id. Maybe you'll be in luck and it'll have a unique one. I'm out of ideas for now.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
Ian
  • 3,500
  • 1
  • 24
  • 25
  • I am not asking about the Quickaction itself, but how to attach that to the search bar – Heiko Rupp Apr 20 '11 at 15:12
  • well just put an onclicklistener() (as the tut does) on the view. In your sample picture above, i guess you would give the imageview of the books an onclicklistener(). – Ian Apr 20 '11 at 18:42
  • THe books icon is the app icon according to http://developer.android.com/guide/topics/search/search-dialog.html – Heiko Rupp Apr 20 '11 at 19:09
  • ah i see. well somehow you need the id of the icon in order to implement the onclicklistener(). you may be stuck just going with a search widget and implementing most by scratch. Or perhaps, with the search dialog implemented and running, use hierarchyviewer to see if the icon has an id. Maybe you'll be in luck and it'll have a unique one. I'm out of ideas for now. – Ian Apr 21 '11 at 01:08
  • Hierarchyviewer is a very good idea here. The image indeed has an id of `id/search_app_icon` – Heiko Rupp Apr 21 '11 at 09:31
  • sweet. obviously now you can identify the view and attach a onclicklistener to activate the quickaction. – Ian Apr 21 '11 at 16:27
1

Those options (and their icons) are determined by the searchable items list in the android settings (Settings->Search->Searchable items at least in my phone). If you want to add a search action to that menu, take a look at this:

http://developer.android.com/guide/topics/search/adding-custom-suggestions.html#QSB

Enabling suggestions on a device

When your application is configured to provide suggestions in Quick Search Box, it is not actually enabled to provide suggestions in Quick Search Box, by default. It is the user's choice whether to include suggestions from your application in the Quick Search Box. To enable search suggestions from your application, the user must open "Searchable items" (in Settings > Search) and enable your application as a searchable item.

Each application that is available to Quick Search Box has an entry in the Searchable items settings page. The entry includes the name of the application and a short description of what content can be searched from the application and made available for suggestions in Quick Search Box. To define the description text for your searchable application, add the android:searchSettingsDescription attribute to your searchable configuration. For example:

http://developer.android.com/guide/topics/search/searchable-config.html

Quick Search Box attributes

To make your custom search suggestions available to Quick Search Box, you need some of the following attributes:

android:includeInGlobalSearch Boolean. (Required to provide search suggestions in Quick Search Box.) Set to "true" if you want your suggestions to be included in the globally accessible Quick Search Box. The user must still enable your application as a searchable item in the system search settings before your suggestions will appear in Quick Search Box. android:searchSettingsDescription String. Provides a brief description of the search suggestions that you provide to Quick Search Box, which is displayed in the searchable items entry for your application. Your description should concisely describe the content that is searchable. For example, "Artists, albums, and tracks" for a music application, or "Saved notes" for a notepad application. android:queryAfterZeroResults Boolean. Set to "true" if you want your content provider to be invoked for supersets of queries that have returned zero results in the past. For example, if your content provider returned zero results for "bo", it should be requiried for "bob". If set to "false", supersets are ignored for a single session ("bob" does not invoke a requery). This lasts only for the life of the search dialog or the life of the activity when using the search widget (when the search dialog or activity is reopened, "bo" queries your content provider again). The default value is false.

So if you want to add an option to search for words or headings, and you have an activity that allows that search, then you can add that Searchable item(s) to the list. They will be available only if the user wants, though.

Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • +1 As I learned about this global "include in search" thingy. But as I wrote, I want that within an application of mine, where I can use the dropdown to select the categories within my to search in. – Heiko Rupp Apr 21 '11 at 18:26
  • OK, I misunderstood the "in an application" part. So you don't want a global search option, but only inside your app. I was trying to find the source code for the SearchView class but it's missing from [platform/frameworks/base.git]/core/java/android/widget/ (or any other place I was looking for) – Aleadam Apr 21 '11 at 20:37
  • @Heiko I found this that might be useful to follow and maybe mimic the implementation: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/app/SearchDialog.java#SearchDialog.SearchBar Edit: Ed found it already :/ – Aleadam Apr 21 '11 at 20:52