public class

SearchView

extends LinearLayout
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.LinearLayout
         ↳ android.widget.SearchView

Class Overview

Provides the user interface elements 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.

XML attributes

See SearchView Attributes, View Attributes

Summary

Nested Classes
interface SearchView.OnCloseListener  
interface SearchView.OnQueryTextListener Callbacks for changes to the query text. 
interface SearchView.OnSuggestionListener Callback interface for selection events on suggestions. 
XML Attributes
Attribute Name Related Method Description
android:iconifiedByDefault The default state of the SearchView. 
android:maxWidth setMaxWidth(int) An optional maximum width of the SearchView. 
[Expand]
Inherited XML Attributes
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
SearchView(Context context)
SearchView(Context context, AttributeSet attrs)
Public Methods
CharSequence getQuery()
Returns the query string currently in the text field.
CursorAdapter getSuggestionsAdapter()
Returns the adapter used for suggestions, if any.
boolean isIconfiedByDefault()
Returns the default iconified state of the search field.
boolean isIconified()
Returns the current iconified state of the SearchView.
boolean isQueryRefinementEnabled()
Returns whether query refinement is enabled for all items or only specific ones.
boolean isSubmitButtonEnabled()
Returns whether the submit button is enabled when necessary or never displayed.
boolean onKeyDown(int keyCode, KeyEvent event)
Handles the key down event for dealing with action keys.
void setIconified(boolean iconify)
Iconifies or expands the SearchView.
void setIconifiedByDefault(boolean iconified)
Sets the default or resting state of the search field.
void setMaxWidth(int maxpixels)
Makes the view at most this many pixels wide
void setOnCloseListener(SearchView.OnCloseListener listener)
Sets a listener to inform when the user closes the SearchView.
void setOnQueryTextFocusChangeListener(View.OnFocusChangeListener listener)
Sets a listener to inform when the focus of the query text field changes.
void setOnQueryTextListener(SearchView.OnQueryTextListener listener)
Sets a listener for user actions within the SearchView.
void setOnSearchClickListener(View.OnClickListener listener)
Sets a listener to inform when the search button is pressed.
void setOnSuggestionListener(SearchView.OnSuggestionListener listener)
Sets a listener to inform when a suggestion is focused or clicked.
void setQuery(CharSequence query, boolean submit)
Sets a query string in the text field and optionally submits the query as well.
void setQueryHint(CharSequence hint)
Sets the hint text to display in the query text field.
void setQueryRefinementEnabled(boolean enable)
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.
void setSearchableInfo(SearchableInfo searchable)
Sets the SearchableInfo for this SearchView.
void setSubmitButtonEnabled(boolean enabled)
Enables showing a submit button when the query is non-empty.
void setSuggestionsAdapter(CursorAdapter adapter)
You can set a custom adapter if you wish.
Protected Methods
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

[Expand]
Inherited Methods
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.ViewManager
From interface android.view.ViewParent
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

android:iconifiedByDefault

Since: API Level

The default state of the SearchView. If true, it will be iconified when not in use and expanded when clicked.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol iconifiedByDefault.

Related Methods

android:maxWidth

Since: API Level

An optional maximum width of the SearchView.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol maxWidth.

Related Methods

Public Constructors

public SearchView (Context context)

Since: API Level 11

public SearchView (Context context, AttributeSet attrs)

Since: API Level 11

Public Methods

public CharSequence getQuery ()

Since: API Level 11

Returns the query string currently in the text field.

Returns
  • the query string

public CursorAdapter getSuggestionsAdapter ()

Since: API Level 11

Returns the adapter used for suggestions, if any.

Returns
  • the suggestions adapter

public boolean isIconfiedByDefault ()

Since: API Level 11

Returns the default iconified state of the search field.

public boolean isIconified ()

Since: API Level 11

Returns the current iconified state of the SearchView.

Returns
  • true if the SearchView is currently iconified, false if the search field is fully visible.

public boolean isQueryRefinementEnabled ()

Since: API Level 11

Returns whether query refinement is enabled for all items or only specific ones.

Returns
  • true if enabled for all items, false otherwise.

public boolean isSubmitButtonEnabled ()

Since: API Level 11

Returns whether the submit button is enabled when necessary or never displayed.

Returns
  • whether the submit button is enabled automatically when necessary

public boolean onKeyDown (int keyCode, KeyEvent event)

Since: API Level 11

Handles the key down event for dealing with action keys.

Parameters
keyCode This is the keycode of the typed key, and is the same value as found in the KeyEvent parameter.
event The complete event record for the typed key
Returns
  • true if the event was handled here, or false if not.

public void setIconified (boolean iconify)

Since: API Level 11

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 a true value will collapse the SearchView to an icon, while a false will expand it.

public void setIconifiedByDefault (boolean iconified)

Since: API Level 11

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.

The default value is true.

Parameters
iconified whether the search field should be iconified by default

public void setMaxWidth (int maxpixels)

Since: API Level 11

Makes the view at most this many pixels wide

Related XML Attributes

public void setOnCloseListener (SearchView.OnCloseListener listener)

Since: API Level 11

Sets a listener to inform when the user closes the SearchView.

Parameters
listener the listener to call when the user closes the SearchView.

public void setOnQueryTextFocusChangeListener (View.OnFocusChangeListener listener)

Since: API Level 11

Sets a listener to inform when the focus of the query text field changes.

Parameters
listener the listener to inform of focus changes.

public void setOnQueryTextListener (SearchView.OnQueryTextListener listener)

Since: API Level 11

Sets a listener for user actions within the SearchView.

Parameters
listener the listener object that receives callbacks when the user performs actions in the SearchView such as clicking on buttons or typing a query.

public void setOnSearchClickListener (View.OnClickListener listener)

Since: API Level 11

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 the listener to inform when the search button is clicked or the text field is programmatically de-iconified.

public void setOnSuggestionListener (SearchView.OnSuggestionListener listener)

Since: API Level 11

Sets a listener to inform when a suggestion is focused or clicked.

Parameters
listener the listener to inform of suggestion selection events.

public void setQuery (CharSequence query, boolean submit)

Since: API Level 11

Sets a query string in the text field and optionally submits the query as well.

Parameters
query the query string. This replaces any query text already present in the text field.
submit whether to submit the query right now or only update the contents of text field.

public void setQueryHint (CharSequence hint)

Since: API Level 11

Sets the hint text to display in the query text field. This overrides any hint specified in the SearchableInfo.

Parameters
hint the hint text to display

public void setQueryRefinementEnabled (boolean enable)

Since: API Level 11

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 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.

public void setSearchableInfo (SearchableInfo searchable)

Since: API Level 11

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 a SearchableInfo can be retrieved from the SearchManager, for a specific activity or a global search provider.

public void setSubmitButtonEnabled (boolean enabled)

Since: API Level 11

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 true to show a submit button for submitting queries, false if a submit button is not required.

public void setSuggestionsAdapter (CursorAdapter adapter)

Since: API Level 11

You can set a custom adapter if you wish. Otherwise the default adapter is used to display the suggestions from the suggestions provider associated with the SearchableInfo.

Protected Methods

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Since: API Level 11

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overriden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.