SearchView
class SearchView : LinearLayoutCompat, CollapsibleActionView
| kotlin.Any | ||||
| ↳ | android.view.View | |||
| ↳ | android.view.ViewGroup | |||
| ↳ | androidx.appcompat.widget.LinearLayoutCompat | |||
| ↳ | androidx.appcompat.widget.SearchView |
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.
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.
If you want the search field to always be visible, then call setIconifiedByDefault(false).
| See also | |
|---|---|
SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW |
Summary
Nested types |
|---|
interface SearchView.OnCloseListener |
interface SearchView.OnQueryTextListenerCallbacks for changes to the query text. |
interface SearchView.OnSuggestionListenerCallback interface for selection events on suggestions. |
Public constructors |
|---|
SearchView(context: Context) |
SearchView(context: Context, attrs: AttributeSet?) |
SearchView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) |
Public functions |
|
|---|---|
Unit |
|
Int |
@Attribute(value = "android:imeOptions")Returns the IME options set on the query text field. |
Int |
Returns the input type set on the query text field. |
Int |
@Attribute(value = "android:maxWidth")Gets the specified maximum width in pixels, if set. |
CharSequence! |
getQuery()Returns the query string currently in the text field. |
CharSequence? |
@Attribute(value = "androidx.appcompat:queryHint")Returns the hint text that will be displayed in the query text field. |
CursorAdapter! |
Returns the adapter used for suggestions, if any. |
Boolean |
@Attribute(value = "androidx.appcompat:iconifiedByDefault")Returns the default iconified state of the search field. |
Boolean |
Returns the current iconified state of the SearchView. |
Boolean |
Returns whether query refinement is enabled for all items or only specific ones. |
Boolean |
Returns whether the submit button is enabled when necessary or never displayed. |
Unit |
Called when this view is collapsed as an action view. |
Unit |
Called when this view is expanded as an action view. |
Unit |
onWindowFocusChanged(hasWindowFocus: Boolean) |
Boolean |
requestFocus(direction: Int, previouslyFocusedRect: Rect!) |
Unit |
setIconified(iconify: Boolean)Iconifies or expands the SearchView. |
Unit |
setIconifiedByDefault(iconified: Boolean)Sets the default or resting state of the search field. |
Unit |
setImeOptions(imeOptions: Int)Sets the IME options on the query text field. |
Unit |
setInputType(inputType: Int)Sets the input type on the query text field. |
Unit |
setMaxWidth(maxpixels: Int)Makes the view at most this many pixels wide |
Unit |
setOnCloseListener(listener: SearchView.OnCloseListener!)Sets a listener to inform when the user closes the SearchView. |
Unit |
Sets a listener to inform when the focus of the query text field changes. |
Unit |
setOnQueryTextListener(listener: SearchView.OnQueryTextListener!)Sets a listener for user actions within the SearchView. |
Unit |
setOnSearchClickListener(listener: View.OnClickListener!)Sets a listener to inform when the search button is pressed. |
Unit |
Sets a listener to inform when a suggestion is focused or clicked. |
Unit |
setQuery(query: CharSequence!, submit: Boolean)Sets a query string in the text field and optionally submits the query as well. |
Unit |
setQueryHint(hint: CharSequence?)Sets the hint text to display in the query text field. |
Unit |
setQueryRefinementEnabled(enable: Boolean)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. |
Unit |
setSearchableInfo(searchable: SearchableInfo!)Sets the SearchableInfo for this SearchView. |
Unit |
setSubmitButtonEnabled(enabled: Boolean)Enables showing a submit button when the query is non-empty. |
Unit |
setSuggestionsAdapter(adapter: CursorAdapter!)You can set a custom adapter if you wish. |
Protected functions |
|
|---|---|
Unit |
|
Unit |
|
Unit |
|
Unit |
onQueryRefine(queryText: CharSequence?)Called when a query refinement has been proposed, e.g. when the user clicks on a suggestion provided by a |
Unit |
onRestoreInstanceState(state: Parcelable!) |
Parcelable! |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Public constructors
Public functions
getImeOptions
@Attribute(value = "android:imeOptions")
fun getImeOptions(): Int
Returns the IME options set on the query text field.
| Returns | |
|---|---|
Int |
the ime options |
| See also | |
|---|---|
setImeOptions |
getInputType
fun getInputType(): Int
Returns the input type set on the query text field.
getMaxWidth
@Attribute(value = "android:maxWidth")
fun getMaxWidth(): Int
Gets the specified maximum width in pixels, if set. Returns zero if no maximum width was specified.
getQuery
fun getQuery(): CharSequence!
Returns the query string currently in the text field.
| Returns | |
|---|---|
CharSequence! |
the query string |
getQueryHint
@Attribute(value = "androidx.appcompat:queryHint")
fun getQueryHint(): CharSequence?
Returns the hint text that will be displayed in the query text field.
The displayed query hint is chosen in the following order:
- Non-null value set with
setQueryHint - Value specified in XML using
app:queryHint - Valid string resource ID exposed by the
SearchableInfoviagetHintId - Default hint provided by the theme against which the view was inflated
| Returns | |
|---|---|
CharSequence? |
the displayed query hint text, or |
getSuggestionsAdapter
fun getSuggestionsAdapter(): CursorAdapter!
Returns the adapter used for suggestions, if any.
| Returns | |
|---|---|
CursorAdapter! |
the suggestions adapter |
isIconfiedByDefault
@Attribute(value = "androidx.appcompat:iconifiedByDefault")
fun isIconfiedByDefault(): Boolean
Returns the default iconified state of the search field.
| Returns | |
|---|---|
Boolean |
isIconified
fun isIconified(): Boolean
Returns the current iconified state of the SearchView.
| Returns | |
|---|---|
Boolean |
true if the SearchView is currently iconified, false if the search field is fully visible. |
isQueryRefinementEnabled
fun isQueryRefinementEnabled(): Boolean
Returns whether query refinement is enabled for all items or only specific ones.
| Returns | |
|---|---|
Boolean |
true if enabled for all items, false otherwise. |
isSubmitButtonEnabled
fun isSubmitButtonEnabled(): Boolean
Returns whether the submit button is enabled when necessary or never displayed.
| Returns | |
|---|---|
Boolean |
whether the submit button is enabled automatically when necessary |
onActionViewCollapsed
fun onActionViewCollapsed(): Unit
Called when this view is collapsed as an action view. See android.view.MenuItem#collapseActionView().
onActionViewExpanded
fun onActionViewExpanded(): Unit
Called when this view is expanded as an action view. See android.view.MenuItem#expandActionView().
setIconified
fun setIconified(iconify: Boolean): Unit
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. 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
fun setIconifiedByDefault(iconified: Boolean): Unit
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: Boolean |
whether the search field should be iconified by default |
setImeOptions
fun setImeOptions(imeOptions: Int): Unit
Sets the IME options on the query text field.
| Parameters | |
|---|---|
imeOptions: Int |
the options to set on the query text field |
| See also | |
|---|---|
setImeOptions |
setInputType
fun setInputType(inputType: Int): Unit
Sets the input type on the query text field.
| See also | |
|---|---|
setInputType |
setMaxWidth
fun setMaxWidth(maxpixels: Int): Unit
Makes the view at most this many pixels wide maxWidth
setOnCloseListener
fun setOnCloseListener(listener: SearchView.OnCloseListener!): Unit
Sets a listener to inform when the user closes the SearchView.
| Parameters | |
|---|---|
listener: SearchView.OnCloseListener! |
the listener to call when the user closes the SearchView. |
setOnQueryTextFocusChangeListener
fun setOnQueryTextFocusChangeListener(listener: View.OnFocusChangeListener!): Unit
Sets a listener to inform when the focus of the query text field changes.
| Parameters | |
|---|---|
listener: View.OnFocusChangeListener! |
the listener to inform of focus changes. |
setOnQueryTextListener
fun setOnQueryTextListener(listener: SearchView.OnQueryTextListener!): Unit
Sets a listener for user actions within the SearchView.
| Parameters | |
|---|---|
listener: SearchView.OnQueryTextListener! |
the listener object that receives callbacks when the user performs actions in the SearchView such as clicking on buttons or typing a query. |
setOnSearchClickListener
fun setOnSearchClickListener(listener: View.OnClickListener!): Unit
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. |
setOnSuggestionListener
fun setOnSuggestionListener(listener: SearchView.OnSuggestionListener!): Unit
Sets a listener to inform when a suggestion is focused or clicked.
| Parameters | |
|---|---|
listener: SearchView.OnSuggestionListener! |
the listener to inform of suggestion selection events. |
setQuery
fun setQuery(query: CharSequence!, submit: Boolean): Unit
Sets a query string in the text field and optionally submits the query as well.
| Parameters | |
|---|---|
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. |
setQueryHint
fun setQueryHint(hint: CharSequence?): Unit
Sets the hint text to display in the query text field. This overrides any hint specified in the SearchableInfo.
This value may be specified as an empty string to prevent any query hint from being displayed.
| Parameters | |
|---|---|
hint: CharSequence? |
the hint text to display or |
setQueryRefinementEnabled
fun setQueryRefinementEnabled(enable: Boolean): Unit
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 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
fun setSearchableInfo(searchable: SearchableInfo!): Unit
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
fun setSubmitButtonEnabled(enabled: Boolean): Unit
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. |
setSuggestionsAdapter
fun setSuggestionsAdapter(adapter: CursorAdapter!): Unit
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.
| See also | |
|---|---|
setSearchableInfo |
Protected functions
onLayout
protected fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int): Unit
onQueryRefine
protected fun onQueryRefine(queryText: CharSequence?): Unit
Called when a query refinement has been proposed, e.g. when the user clicks on a suggestion provided by a SuggestionsAdapter.
By default, this method sets the text in the query box without updating the suggestions.
| Parameters | |
|---|---|
queryText: CharSequence? |
the proposed query refinement |