Android helps you implement the user interface with either a search dialog that appears at the top of the activity window or a search widget that you can insert in your layout.
Other features available for the search dialog and widget include:
- Voice search
- Search suggestions based on recent queries
- Search suggestions that match actual results in your application data
Note: If you want, you can handle all user input into the search widget yourself, using various callback methods and listeners. This document, however, focuses on how to integrate the search widget with the system for an assisted search implementation. If you want to handle all user input yourself, read the reference documentation for SearchView
and its nested interfaces.
> The searchable configuration file must include the <searchable>
element as the root node and specify one or more attributes. For example:
<?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_label" android:hint="@string/search_hint" > </searchable>> You don't need to implement the search functionality yet—just create an activity that you can declare in the manifest. Inside the manifest's
<activity>
element:
- Declare the activity to accept the
ACTION_SEARCH
intent, in an<intent-filter>
element. - Specify the searchable configuration to use, in a
<meta-data>
element.
For example:
<application ... > <activity android:name=".SearchableActivity" > <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> </activity> ... </application>
Intent intent = getIntent(); if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); doMySearch(query); }> The search dialog provides a floating search box at the top of the screen, with the application icon on the left. The search dialog can provide search suggestions as the user types and, when the user executes a search, the system sends the search query to a searchable activity that performs the search. However, if you are developing your application for devices running Android 3.0, you should consider using the search widget instead (see the side box).
<application ... > <!-- this is the searchable activity; it performs searches --> <activity android:name=".SearchableActivity" > <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> </activity> <!-- this activity enables the search dialog to initiate searches in the SearchableActivity --> <activity android:name=".OtherActivity" ... > <!-- enable the search dialog to send searches to SearchableActivity --> <meta-data android:name="android.app.default_searchable" android:value=".SearchableActivity" /> </activity> ... </application>> If you want every activity in your application to provide the search dialog, insert the above
<meta-data>
element as a child of the
<application>
element, instead of each
<activity>
.
Note: If your app uses an app bar, then you should not use the search dialog for your search interface. Instead, use the search widget as a collapsible view in the app bar.
. If, however, the current activity is the searchable activity, then one of two things happens:
- By default, the searchable activity receives the
ACTION_SEARCH
intent with a call toonCreate()
and a new instance of the activity is brought to the top of the activity stack. There are now two instances of your searchable activity in the activity stack (so pressing the Back button goes back to the previous instance of the searchable activity, rather than exiting the searchable activity). - If you set
android:launchMode
to"singleTop"
, then the searchable activity receives theACTION_SEARCH
intent with a call toonNewIntent(Intent)
, passing the newACTION_SEARCH
intent here. For example, here's how you might handle this case, in which the searchable activity's launch mode is"singleTop"
:@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search); handleIntent(getIntent()); } @Override protected void onNewIntent(Intent intent) { setIntent(intent); handleIntent(intent); } private void handleIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); doMySearch(query); } }
For example, if you're using a SearchView
as an action view in the app bar, you should enable the widget during the onCreateOptionsMenu()
callback:
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the options menu from XML MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.options_menu, menu); // Get the SearchView and set the searchable configuration SearchManager searchManager = (SearchManager)Note: Carefully consider whether voice search is appropriate for your application. All searches performed with the voice search button are immediately sent to your searchable activity without a chance for the user to review the transcribed query. Sufficiently test the voice recognition and ensure that it understands the types of queries that the user might submit inside your application.getSystemService
(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); // Assumes current activity is the searchable activity searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()
)); searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default return true; }