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