1page.title=Searchable Configuration 2 3@jd:body 4 5<div id="qv-wrapper"> 6<div id="qv"> 7<h2>See also</h2> 8<ol> 9 <li><a href="search-dialog.html">Creating a Search Interface</a></li> 10 <li><a href="adding-recent-query-suggestions.html">Adding Recent Query Suggestions</a></li> 11 <li><a href="adding-custom-suggestions.html">Adding Custom Suggestions</a></li> 12</ol> 13</div> 14</div> 15 16<p>In order to implement search with assistance from the Android system (to deliver search queries 17to an activity and provide search suggestions), your application must provide a search configuration 18in the form of an XML file.</p> 19 20<p>This page describes the search configuration file in terms of its syntax and usage. For more 21information about how to implement search features for your application, begin with the developer 22guide about <a href="search-dialog.html">Creating a Search Interface</a>.</p> 23 24<dl class="xml"> 25 26<dt>file location:</dt> 27<dd><code>res/xml/<em>filename</em>.xml</code><br/> 28Android uses the filename as the resource ID.</dd> 29 30<dt>syntax:</dt> 31<dd> 32<pre class="stx"> 33<?xml version="1.0" encoding="utf-8"?> 34<<a href="#searchable-element">searchable</a> xmlns:android="http://schemas.android.com/apk/res/android" 35 android:<a href="#label">label</a>="<em>string resource</em>" 36 android:<a href="#hint">hint</a>="<em>string resource</em>" 37 android:<a href="#searchMode">searchMode</a>=["queryRewriteFromData" | "queryRewriteFromText"] 38 android:<a href="#searchButtonText">searchButtonText</a>="<em>string resource</em>" 39 android:<a href="#inputType">inputType</a>="<em>{@link android.R.attr#inputType}</em>" 40 android:<a href="#imeOptions">imeOptions</a>="<em>{@link android.R.attr#imeOptions}</em>" 41 android:<a href="#searchSuggestAuthority">searchSuggestAuthority</a>="<em>string</em>" 42 android:<a href="#searchSuggestPath">searchSuggestPath</a>="<em>string</em>" 43 android:<a href="#searchSuggestSelection">searchSuggestSelection</a>="<em>string</em>" 44 android:<a href="#searchSuggestIntentAction">searchSuggestIntentAction</a>="<em>string</em>" 45 android:<a href="#searchSuggestIntentData">searchSuggestIntentData</a>="<em>string</em>" 46 android:<a href="#searchSuggestThreshold">searchSuggestThreshold</a>="<em>int</em>" 47 android:<a href="#includeInGlobalSearch">includeInGlobalSearch</a>=["true" | "false"] 48 android:<a href="#searchSettingsDescription">searchSettingsDescription</a>="<em>string resource</em>" 49 android:<a href="#queryAfterZeroResults">queryAfterZeroResults</a>=["true" | "false"] 50 android:<a href="#voiceSearchMode">voiceSearchMode</a>=["showVoiceSearchButton" | "launchWebSearch" | "launchRecognizer"] 51 android:<a href="#voiceLanguageModel">voiceLanguageModel</a>=["free-form" | "web_search"] 52 android:<a href="#voicePromptText">voicePromptText</a>="<em>string resource</em>" 53 android:<a href="#voiceLanguage">voiceLanguage</a>="<em>string</em>" 54 android:<a href="#voiceMaxResults">voiceMaxResults</a>="<em>int</em>" 55 > 56 <<a href="#actionkey-element">actionkey</a> 57 android:keycode="<em>{@link android.view.KeyEvent KEYCODE}</em>" 58 android:queryActionMsg="<em>string</em>" 59 android:suggestActionMsg="<em>string</em>" 60 android:suggestActionMsgColumn="<em>string</em>" > 61</searchable> 62</pre> 63</dd> 64 65<dt>elements:</dt> 66<dd> 67<dl class="tag-list"> 68 <dt id="searchable-element"><code><searchable></code></dt> 69 <dd>Defines all search configurations used by the Android system to provide assisted search. 70 <p class="caps">attributes:</p> 71 <dl class="atn-list"> 72 <dt><a name="label"></a><code>android:label</code></dt> 73 <dd><em>String resource</em>. (Required.) The name of your application. 74It should be the same as the name applied to the {@code android:label} attribute of your <a 75href="{@docRoot}guide/topics/manifest/activity-element.html#label">{@code <activity>}</a> or 76<a href="{@docRoot}guide/topics/manifest/application-element.html#label">{@code 77<application>}</a> manifest element. This label is only visible to the user when you set 78<code>android:includeInGlobalSearch</code> to "true", in which case, this label is used to identify 79your application as a searchable item in the system's search settings.</dd> 80 81 <dt><a name="hint"></a><code>android:hint</code></dt> 82 <dd><em>String resource</em>. (Recommended.) The text to display in the search text field when 83no text has been entered. It provides a hint to the user about what 84content is searchable. For consistency with other Android applications, you should format the 85string for {@code android:hint} as "Search <em><content-or-product></em>". For example, 86"Search songs and artists" or "Search YouTube".</dd> 87 88 <dt><a name="searchMode"></a><code>android:searchMode</code></dt> 89 <dd><em>Keyword</em>. Sets additional modes that control the search presentation. 90Currently available modes define how the query text should be rewritten when a custom suggestion 91receives focus. The following mode values are accepted: 92 <table> 93 <tr><th>Value</th><th>Description</th></tr> 94 <tr> 95 <td><code>"queryRewriteFromText"</code></td> 96 <td>Use the value from the {@link android.app.SearchManager#SUGGEST_COLUMN_TEXT_1} 97column to rewrite the query text.</td> 98 </tr> 99 <tr> 100 <td><code>"queryRewriteFromData"</code></td> 101 <td>Use the value from the 102 {@link android.app.SearchManager#SUGGEST_COLUMN_INTENT_DATA} column to rewrite the 103query text. This should only be used when the values in 104 {@link android.app.SearchManager#SUGGEST_COLUMN_INTENT_DATA} are suitable for user 105inspection and editing, typically HTTP URI's.</td> 106 </tr> 107 </table> 108 <p>For more information, see the discussion about rewriting the query text in <a 109href="adding-custom-suggestions.html#RewritingQueryText">Adding Custom Suggestions</a>.</p> 110 </dd> 111 112 <dt><a name="searchButtonText"></a><code>android:searchButtonText</code></dt> 113 <dd><em>String resource</em>. The text to display in the button that executes search. By 114default, the button shows a search icon (a magnifying glass), which is ideal for 115internationalization, so you should not use this attribute to change the button unless the 116behavior is something other than a search (such as a URL request in a web browser).</dd> 117 118 <dt><a name="inputType"></a><code>android:inputType</code></dt> 119 <dd><em>Keyword</em>. Defines the type of input method (such as the type of soft keyboard) 120to use. For most searches, in which free-form text is expected, you don't 121need this attribute. See {@link android.R.attr#inputType} for a list of suitable values for this 122attribute.</dd> 123 124 <dt><a name="imeOptions"></a><code>android:imeOptions</code></dt> 125 <dd><em>Keyword</em>. Supplies additional options for the input method. 126 For most searches, in which free-form text is expected, you don't need this attribute. The 127default IME is "actionSearch" (provides the "search" button instead of a carriage 128return in the soft keyboard). See {@link android.R.attr#imeOptions} for a list of suitable values 129for this attribute. 130 </dd> 131 </dl> 132 133 134 <h4>Search suggestion attributes</h4> 135 136 <p>If you have defined a content provider to generate search suggestions, you need to 137 define additional attributes that configure communications with the content 138 provider. When providing search suggestions, you need some of the following 139 {@code <searchable>} attributes:</p><br/> 140 141 <dl class="atn-list"> 142 <dt><a name="searchSuggestAuthority"></a><code>android:searchSuggestAuthority</code></dt> 143 <dd><em>String</em>. (Required to provide search suggestions.) 144 This value must match the authority string provided in the {@code android:authorities} 145attribute of the Android manifest {@code <provider>} element.</dd> 146 147 <dt><a name="searchSuggestPath"></a><code>android:searchSuggestPath</code></dt> 148 <dd><em>String</em>. This path is used as a portion of the suggestions 149 query {@link android.net.Uri}, after the prefix and authority, but before 150the standard suggestions path. 151 This is only required if you have a single content provider issuing different types 152 of suggestions (such as for different data types) and you need 153 a way to disambiguate the suggestions queries when you receive them.</dd> 154 155 <dt><a name="searchSuggestSelection"></a><code>android:searchSuggestSelection</code></dt> 156 <dd><em>String</em>. This value is passed into your 157 query function as the {@code selection} parameter. Typically this is a WHERE clause 158for your database, and should contain a single question mark, which is a placeholder for the 159actual query string that has been typed by the user (for example, {@code "query=?"}). However, you 160can also use any non-null value to trigger the delivery of the query text via the {@code 161selectionArgs} parameter (and then ignore the {@code selection} parameter).</dd> 162 163 <dt><a name="searchSuggestIntentAction"></a><code>android:searchSuggestIntentAction</code></dt> 164 <dd><em>String</em>. The default intent action to be used when a user 165 clicks on a custom search suggestion (such as {@code "android.intent.action.VIEW"}). 166 If this is not overridden by the selected suggestion (via the {@link 167android.app.SearchManager#SUGGEST_COLUMN_INTENT_ACTION} column), this value is placed in the action 168field of the {@link android.content.Intent} when the user clicks a suggestion.</dd> 169 170 <dt><a name="searchSuggestIntentData"></a><code>android:searchSuggestIntentData</code></dt> 171 <dd><em>String</em>. The default intent data to be used when a user 172 clicks on a custom search suggestion. 173 If not overridden by the selected suggestion (via the {@link 174android.app.SearchManager#SUGGEST_COLUMN_INTENT_DATA} column), this value is 175 placed in the data field of the {@link android.content.Intent} when the user clicks 176 a suggestion.</dd> 177 178 <dt><a name="searchSuggestThreshold"></a><code>android:searchSuggestThreshold</code></dt> 179 <dd><em>Integer</em>. The minimum number of characters needed to 180 trigger a suggestion look-up. Only guarantees that the system will not query your 181 content provider for anything shorter than the threshold. The default value is 0.</dd> 182 </dl> 183 184 <p>For more information about the above attributes for search suggestions, see the guides for 185 <a href="adding-recent-query-suggestions.html">Adding Recent Query Suggestions</a> and 186 <a href="adding-custom-suggestions.html">Adding Custom Suggestions</a>.</p> 187 188 189 <h4>Quick Search Box attributes</h4> 190 191 <p>To make your custom search suggestions available to Quick Search Box, you need some of the 192 following {@code <searchable>} attributes:</p><br/> 193 194 <dl class="atn-list"> 195 <dt><a name="includeInGlobalSearch"></a><code>android:includeInGlobalSearch</code></dt> 196 <dd><em>Boolean</em>. (Required to provide search suggestions in 197 Quick Search Box.) Set to "true" if you want your suggestions to be 198 included in the globally accessible Quick Search Box. The user must 199 still enable your application as a searchable item in the system search settings before 200 your suggestions will appear in Quick Search Box.</dd> 201 202 <dt><a name="searchSettingsDescription"></a><code>android:searchSettingsDescription</code></dt> 203 <dd><em>String</em>. Provides a brief description of the search suggestions that you provide 204to Quick Search Box, which is displayed in the searchable items entry for your application. 205Your description should concisely describe the content that is searchable. For example, "Artists, 206albums, and tracks" for a music application, or "Saved notes" for a notepad application.</dd> 207 208 <dt><a name="queryAfterZeroResults"></a><code>android:queryAfterZeroResults</code></dt> 209 <dd><em>Boolean</em>. Set to "true" if you want your content provider to be invoked for 210 supersets of queries that have returned zero results in the past. For example, if 211your content provider returned zero results for "bo", it should be requiried for "bob". If set to 212"false", supersets are ignored for a single session ("bob" does not invoke a requery). This lasts 213only for the life of the search dialog or the life of the activity when using the search widget 214(when the search dialog or activity is reopened, "bo" queries your 215content provider again). The default value is false.</dd> 216 </dl> 217 218 219 <h4>Voice search attributes</h4> 220 221 <p>To enable voice search, you'll need some of the 222 following {@code <searchable>} attributes:</p><br/> 223 224 <dl class="atn-list"> 225 <dt><a name="voiceSearchMode"></a><code>android:voiceSearchMode</code></dt> 226 <dd><em>Keyword</em>. (Required to provide voice search capabilities.) 227 Enables voice search, with a specific mode for voice search. 228 (Voice search may not be provided by the device, in which case these flags 229 have no effect.) The following mode values are accepted: 230 <table> 231 <tr><th>Value</th><th>Description</th></tr> 232 <tr> 233 <td><code>"showVoiceSearchButton"</code></td> 234 <td>Display a voice search button, if voice search is available on the device. If set, 235then either {@code "launchWebSearch"} or {@code "launchRecognizer"} must also be set 236 (separated by the pipe | character).</td> 237 </tr> 238 <tr> 239 <td><code>"launchWebSearch"</code></td> 240 <td>The voice search button takes the user directly 241 to a built-in voice web search activity. Most applications don't need this flag, as 242 it takes the user away from the activity in which search was invoked.</td> 243 </tr> 244 <tr> 245 <td><code>"launchRecognizer"</code></td> 246 <td>The voice search button takes 247 the user directly to a built-in voice recording activity. This activity 248 prompts the user to speak, transcribes the spoken text, and forwards the resulting 249 query text to the searchable activity, just as if the user typed it into the 250 search UI and clicked the search button.</td> 251 </tr> 252 </table> 253 </dd> 254 255 <dt><a name="voiceLanguageModel"></a><code>android:voiceLanguageModel</code></dt> 256 <dd><em>Keyword</em>. The language model that 257 should be used by the voice recognition system. The following values are accepted: 258 <table> 259 <tr><th>Value</th><th>Description</th></tr> 260 <tr> 261 <td><code>"free_form"</code></td> 262 <td>Use free-form speech recognition for dictating queries. This is primarily 263optimized for English. This is the default.</td> 264 </tr> 265 <tr> 266 <td><code>"web_search"</code></td> 267 <td>Use web-search-term recognition for shorter, search-like phrases. This is 268available in more languages than "free_form".</td> 269 </tr> 270 </table> 271 <p>Also see 272 {@link android.speech.RecognizerIntent#EXTRA_LANGUAGE_MODEL} for more 273 information.</p></dd> 274 275 <dt><a name="voicePromptText"></a><code>android:voicePromptText</code></dt> 276 <dd><em>String</em>. An additional message to display in the voice input dialog.</dd> 277 278 <dt><a name="voiceLanguage"></a><code>android:voiceLanguage</code></dt> 279 <dd><em>String</em>. The spoken language to be expected, expressed as the string value of 280a constants in {@link java.util.Locale} (such as {@code "de"} for German or {@code "fr"} for 281French). This is needed only if it is different from the current value of {@link 282java.util.Locale#getDefault() Locale.getDefault()}.</dd> 283 284 <dt><a name="voiceMaxResults"></a><code>android:voiceMaxResults</code></dt> 285 <dd><em>Integer</em>. Forces the maximum number of results to return, 286 including the "best" result which is always provided as the {@link 287android.content.Intent#ACTION_SEARCH} intent's primary 288 query. Must be 1 or greater. Use {@link android.speech.RecognizerIntent#EXTRA_RESULTS} to 289get the results from the intent. 290 If not provided, the recognizer chooses how many results to return.</dd> 291 </dl> 292 </dd> <!-- end searchable element --> 293 294 295 296 <dt id="actionkey-element"><code><actionkey></code></dt> 297 <dd>Defines a device key and behavior for a search action. A search action provides a 298special behavior at the touch of a button on the device, based on the current query or focused 299suggestion. For example, the Contacts application provides a search action to initiate a phone call 300to the currenly focused contact suggestion at the press of the CALL button. 301 <p>Not all action keys are available on every device, and not 302all keys are allowed to be overriden in this way. For example, the "Home" key cannot be used and 303must always return to the home screen. Also be sure not to define an action 304key for a key that's needed for typing a search query. This essentially limits the 305available and reasonable action keys to the call button and menu button. Also note that action 306keys are not generally discoverable, so you should not provide them as a core user feature.</p> 307 <p>You must define the <code>android:keycode</code> to define the key and at least one of the 308other three attributes in order to define the search action.</p> 309 <p class="caps">attributes:</p> 310 <dl class="atn-list"> 311 <dt><a name="keycode"></a><code>android:keycode</code></dt> 312 <dd><em>String</em>. (Required.) A key code from {@link 313android.view.KeyEvent} that represents the action key 314 you wish to respond to (for example {@code "KEYCODE_CALL"}). This is added to the 315 {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} intent that is passed to your 316 searchable activity. To examine the key code, use 317 {@link android.content.Intent#getIntExtra getIntExtra(SearchManager.ACTION_KEY)}. Not all 318keys are supported for a search action, as many of them are used for typing, navigation, or system 319functions.</dd> 320 321 <dt><a name="queryActionMsg"></a><code>android:queryActionMsg</code></dt> 322 <dd><em>String</em>. An action message to be sent if the action key is pressed while the 323user is entering query text. This is added to the 324 {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} intent that the system 325 passes to your searchable activity. To examine the string, use 326 {@link android.content.Intent#getStringExtra 327 getStringExtra(SearchManager.ACTION_MSG)}.</dd> 328 329 <dt><a name="suggestActionMsg"></a><code>android:suggestActionMsg</code></dt> 330 <dd><em>String</em>. An action message to be sent if the action key is pressed while a 331 suggestion is in focus. This is added to the 332 intent that the system passes to your searchable activity (using the action 333you've defined for the suggestion). To examine the string, 334 use {@link android.content.Intent#getStringExtra 335 getStringExtra(SearchManager.ACTION_MSG)}. This should only be used if all your 336suggestions support this action key. If not all suggestions can handle the same action key, then 337you must instead use the following {@code android:suggestActionMsgColumn} attribute.</dd> 338 339 <dt><a name="suggestActionMsgColumn"></a><code>android:suggestActionMsgColumn</code></dt> 340 <dd><em>String</em>. The name of the column in your content provider that defines the 341action message for this action key, which is to be sent if the user presses the action key while a 342 suggestion is in focus. This attribute lets you control the 343action key on a suggestion-by-suggestion basis, because, instead of using the {@code 344android:suggestActionMsg} attribute to define the action message for all suggestions, each entry in 345your content provider provides its own action message. 346 <p>First, you must define a column in your 347content provider for each suggestion to provide an action message, then provide the name of that 348column in this attribute. The system looks at your suggestion cursor, 349 using the string provided here to select your action message column, and 350 then select the action message string from the Cursor. That string is added to the 351 intent that the system passes to your searchable activity (using the action you've 352defined for suggestions). To examine the string, use {@link 353android.content.Intent#getStringExtra getStringExtra(SearchManager.ACTION_MSG)}. If the data 354does not exist for the selected suggestion, the action key is ignored.</dd> 355 </dl> 356 </dd><!-- end action key --> 357 </dl> 358</dd><!-- end elements --> 359 360 361<dt>example:</dt> 362<dd>XML file saved at <code>res/xml/searchable.xml</code>: 363<pre> 364<?xml version="1.0" encoding="utf-8"?> 365<searchable xmlns:android="http://schemas.android.com/apk/res/android" 366 android:label="@string/search_label" 367 android:hint="@string/search_hint" 368 android:searchSuggestAuthority="dictionary" 369 android:searchSuggestIntentAction="android.intent.action.VIEW" 370 android:includeInGlobalSearch="true" 371 android:searchSettingsDescription="@string/settings_description" > 372</searchable> 373</pre> 374 375</dd> <!-- end example --> 376 377 378</dl> 379 380 381 382 383