Android APIs
public final class

SearchViewCompat

extends Object
java.lang.Object
   ↳ android.support.v4.widget.SearchViewCompat

Class Overview

Helper for accessing features in SearchView introduced after API level 4 in a backwards compatible fashion.

Summary

Nested Classes
class SearchViewCompat.OnCloseListenerCompat Callback for closing the query UI. 
class SearchViewCompat.OnQueryTextListenerCompat Callbacks for changes to the query text. 
Public Methods
static CharSequence getQuery(View searchView)
Returns the query string currently in the text field.
static boolean isIconified(View searchView)
Returns the current iconified state of the SearchView.
static boolean isQueryRefinementEnabled(View searchView)
Returns whether query refinement is enabled for all items or only specific ones.
static boolean isSubmitButtonEnabled(View searchView)
Returns whether the submit button is enabled when necessary or never displayed.
static View newSearchView(Context context)
Creates a new SearchView.
static void setIconified(View searchView, boolean iconify)
Iconifies or expands the SearchView.
static void setImeOptions(View searchView, int imeOptions)
Sets the IME options on the query text field.
static void setInputType(View searchView, int inputType)
Sets the input type on the query text field.
static void setMaxWidth(View searchView, int maxpixels)
Makes the view at most this many pixels wide
static void setOnCloseListener(View searchView, SearchViewCompat.OnCloseListenerCompat listener)
Sets a listener to inform when the user closes the SearchView.
static void setOnQueryTextListener(View searchView, SearchViewCompat.OnQueryTextListenerCompat listener)
Sets a listener for user actions within the SearchView.
static void setQuery(View searchView, CharSequence query, boolean submit)
Sets a query string in the text field and optionally submits the query as well.
static void setQueryHint(View searchView, CharSequence hint)
Sets the hint text to display in the query text field.
static void setQueryRefinementEnabled(View searchView, 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.
static void setSearchableInfo(View searchView, ComponentName searchableComponent)
Sets the SearchableInfo for this SearchView.
static void setSubmitButtonEnabled(View searchView, boolean enabled)
Enables showing a submit button when the query is non-empty.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public static CharSequence getQuery (View searchView)

Returns the query string currently in the text field.

Parameters
searchView View: The SearchView to operate on.
Returns
CharSequence the query string

public static boolean isIconified (View searchView)

Returns the current iconified state of the SearchView.

Parameters
searchView View: The SearchView to operate on.
Returns
boolean true if the SearchView is currently iconified, false if the search field is fully visible.

public static boolean isQueryRefinementEnabled (View searchView)

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

Parameters
searchView View: The SearchView to operate on.
Returns
boolean true if enabled for all items, false otherwise.

public static boolean isSubmitButtonEnabled (View searchView)

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

Parameters
searchView View: The SearchView to operate on.
Returns
boolean whether the submit button is enabled automatically when necessary

public static View newSearchView (Context context)

Creates a new SearchView.

Parameters
context Context: The Context the view is running in.
Returns
View A SearchView instance if the class is present on the current platform, null otherwise.

public static void setIconified (View searchView, boolean iconify)

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
searchView View: The SearchView to operate on.
iconify boolean: a true value will collapse the SearchView to an icon, while a false will expand it.

public static void setImeOptions (View searchView, int imeOptions)

Sets the IME options on the query text field. This is a no-op if called on pre-ICE_CREAM_SANDWICH platforms.

Parameters
searchView View: The SearchView to operate on.
imeOptions int: the options to set on the query text field

See also:

public static void setInputType (View searchView, int inputType)

Sets the input type on the query text field. This is a no-op if called on pre-ICE_CREAM_SANDWICH platforms.

Parameters
searchView View: The SearchView to operate on.
inputType int: the input type to set on the query text field

See also:

public static void setMaxWidth (View searchView, int maxpixels)

Makes the view at most this many pixels wide

Parameters
searchView View: The SearchView to operate on.
maxpixels int

public static void setOnCloseListener (View searchView, SearchViewCompat.OnCloseListenerCompat listener)

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

Parameters
searchView View: The SearchView in which to register the listener.
listener SearchViewCompat.OnCloseListenerCompat: the listener to call when the user closes the SearchView.

public static void setOnQueryTextListener (View searchView, SearchViewCompat.OnQueryTextListenerCompat listener)

Sets a listener for user actions within the SearchView.

Parameters
searchView View: The SearchView in which to register the listener.
listener SearchViewCompat.OnQueryTextListenerCompat: the listener object that receives callbacks when the user performs actions in the SearchView such as clicking on buttons or typing a query.

public static void setQuery (View searchView, CharSequence query, boolean submit)

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

Parameters
searchView View: The SearchView to operate on.
query CharSequence: the query string. This replaces any query text already present in the text field.
submit boolean: whether to submit the query right now or only update the contents of text field.

public static void setQueryHint (View searchView, CharSequence hint)

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

Parameters
searchView View: The SearchView to operate on.
hint CharSequence: the hint text to display

public static void setQueryRefinementEnabled (View searchView, 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. 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(View, ComponentName) and not when using a custom adapter.

Parameters
searchView View: The SearchView to operate on.
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.

public static void setSearchableInfo (View searchView, ComponentName searchableComponent)

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
searchView View: The SearchView to operate on.
searchableComponent ComponentName: The application component whose SearchableInfo should be loaded and applied to the SearchView.

public static void setSubmitButtonEnabled (View searchView, boolean enabled)

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
searchView View: The SearchView to operate on.
enabled boolean: true to show a submit button for submitting queries, false if a submit button is not required.