SearchView

This package is part of the
Android support library which
is no longer maintained.
The support library has been superseded by AndroidX
which is part of Jetpack.
We recommend using the AndroidX libraries in all new projects. You should also consider
migrating existing projects to AndroidX.

A widget that provides a user interface for the user to enter a search query and submit a request
to a search provider. Shows a list of query suggestions or results, if available, and allows the
user to pick a suggestion or result to launch into.

Note: This class is included in the support library for compatibility
with API level 7 and higher. If you're developing your app for API level 11 and higher
only, you should instead use the framework SearchView class.

When the SearchView is used in an ActionBar
as an action view, it's collapsed by default, so you must provide an icon for the action.

requestFocus

setIconified

Iconifies or expands the SearchView. Any query text is cleared when iconified. This is
a temporary state and does not override the default iconified state set by
setIconifiedByDefault(boolean). If the default state is iconified, then
a false here will only be valid until the user closes the field. And if the default
state is expanded, then a true here will only clear the text field and not close it.

Parameters

iconify

boolean: a true value will collapse the SearchView to an icon, while a false will
expand it.

setIconifiedByDefault

Sets the default or resting state of the search field. If true, a single search icon is
shown by default and expands to show the text field and other buttons when pressed. Also,
if the default state is iconified, then it collapses to that state when the close button
is pressed. Changes to this property will take effect immediately.

setOnSearchClickListener

Sets a listener to inform when the search button is pressed. This is only
relevant when the text field is not visible by default. Calling setIconified(false) can also cause this listener to be informed.

Parameters

listener

View.OnClickListener: the listener to inform when the search button is clicked or
the text field is programmatically de-iconified.

setQueryRefinementEnabled

Specifies if a query refinement button should be displayed alongside each suggestion
or if it should depend on the flags set in the individual items retrieved from the
suggestions provider. Clicking on the query refinement button will replace the text
in the query text field with the text from the suggestion. This flag only takes effect
if a SearchableInfo has been specified with setSearchableInfo(SearchableInfo)
and not when using a custom adapter.

Parameters

enable

boolean: true if all items should have a query refinement button, false if only
those items that have a query refinement flag set should have the button.

setSearchableInfo

Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used
to display labels, hints, suggestions, create intents for launching search results screens
and controlling other affordances such as a voice button.

Parameters

searchable

SearchableInfo: a SearchableInfo can be retrieved from the SearchManager, for a specific
activity or a global search provider.

setSubmitButtonEnabled

Enables showing a submit button when the query is non-empty. In cases where the SearchView
is being used to filter the contents of the current activity and doesn't launch a separate
results activity, then the submit button should be disabled.

Parameters

enabled

boolean: true to show a submit button for submitting queries, false if a submit
button is not required.