SearchView

public class SearchView


Layout that provides a full screen search view and can be used with SearchBar.

The example below shows how to use the SearchBar and SearchView together:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <!-- NestedScrollingChild goes here (NestedScrollView, RecyclerView, etc.). -->
  <androidx.core.widget.NestedScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/searchbar_scrolling_view_behavior">
    <!-- Screen content goes here. -->
  </androidx.core.widget.NestedScrollView>

  <com.google.android.material.appbar.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
    <com.google.android.material.search.SearchBar
        android:id="@+id/search_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/searchbar_hint" />
  </com.google.android.material.appbar.AppBarLayout>

  <com.google.android.material.search.SearchView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:hint="@string/searchbar_hint"
      app:layout_anchor="@id/search_bar">
    <!-- Search suggestions/results go here (ScrollView, RecyclerView, etc.). -->
  </com.google.android.material.search.SearchView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

For more information, see the component developer guidance and design guidelines.

Summary

Nested types

public class SearchView.Behavior

Behavior that sets up an SearchView with an SearchBar.

Callback interface that provides important transition events for a SearchView.

Enum that defines the possible transition states of an SearchView.

Public fields

SearchView.TransitionState
final EditText
final TextView
int

Public constructors

SearchView(Context context)
SearchView(Context context, AttributeSet attrs)
SearchView(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

void
addHeaderView(View headerView)

Add a header view to this SearchView, which will be placed above the search text area.

void

Adds a listener to handle SearchView transitions such as showing and closing.

void
addView(View child, int index, LayoutParams params)
void

Call this method from onBackCancelled or handleOnBackCancelled so that the back handler can cancel the back animation.

void

Clears focus on the main EditText and hides the soft keyboard.

void

Clears the text of main EditText.

Behavior<SearchView>
SearchView.TransitionState

Returns the current TransitionState for this SearchView.

EditText

Returns the main EditText which can be used for hint and search text.

CharSequence

Returns the hint of main EditText.

TextView

Returns the search prefix TextView, which appears before the main EditText.

CharSequence

Returns the search prefix text.

int

Returns the current value of this SearchView's soft input mode.

Editable

Returns the text of main EditText, which usually represents the search text.

Toolbar

Returns the Toolbar used by the SearchView.

void

Call this method from onBackInvoked or handleOnBackPressed so that the back handler can complete the back animation, or handle back without progress in certain cases.

void

Hides the SearchView with an animation.

void
inflateMenu(int menuResId)

Inflate a menu to provide additional options.

boolean

Returns whether the navigation icon should be animated from the SearchBar to .

boolean

Returns whether the soft keyboard should be shown when the SearchView is shown.

boolean

Returns whether the menu items should be animated from the SearchBar to .

boolean

Returns whether or not this SearchView is set up with an SearchBar.

boolean

Returns whether the SearchView's main content view is shown or showing.

void

Remove all header views from the section above the search text area.

void
removeHeaderView(View headerView)

Remove a header view from the section above the search text area.

void

Removes a listener to handle SearchView transitions such as showing and closing.

void

Requests focus on the main EditText and shows the soft keyboard.

void
setAnimatedNavigationIcon(boolean animatedNavigationIcon)

Sets whether the navigation icon should be animated from the SearchBar to .

void
setAutoShowKeyboard(boolean autoShowKeyboard)

Sets whether the soft keyboard should be shown when the SearchView is shown.

void
setElevation(float elevation)
void

Sets the hint of main EditText.

void
setHint(int hintResId)

Sets the hint of main EditText.

void
setMenuItemsAnimated(boolean menuItemsAnimated)

Sets whether the menu items should be animated from the SearchBar to .

void
setModalForAccessibility(boolean isSearchViewModal)

Sets whether the SearchView is modal for accessibility, i.e., whether views that are not nested within the SearchView are important for accessibility.

void
setOnMenuItemClickListener(
    OnMenuItemClickListener onMenuItemClickListener
)

Set a listener to handle menu item clicks.

void

Sets the search prefix text.

void

Sets the text of main EditText.

void
setText(int textResId)

Sets the text of main EditText.

void
setToolbarTouchscreenBlocksFocus(boolean touchscreenBlocksFocus)

Sets the 'touchscreenBlocksFocus' attribute of the nested toolbar.

void
setVisible(boolean visible)

Updates the visibility of the SearchView without an animation.

void

Sets up this SearchView with an SearchBar, which will result in the being shown when the SearchBar is clicked.

void

Shows the SearchView with an animation.

void
startBackProgress(BackEventCompat backEvent)

Call this method from onBackStarted or handleOnBackStarted so that the back handler can initialize and start animating.

void
updateBackProgress(BackEventCompat backEvent)

Call this method from onBackProgressed or handleOnBackProgressed so that the back handler can continue animating with a new progress value.

void

Sets the soft input mode for this SearchView.

Protected methods

void
void
void
void
onRestoreInstanceState(Parcelable state)
Parcelable

Public fields

currentTransitionState

public SearchView.TransitionState currentTransitionState

editText

public final EditText editText

searchPrefix

public final TextView searchPrefix

softInputMode

public int softInputMode

Public constructors

SearchView

public SearchView(Context context)

SearchView

public SearchView(Context context, AttributeSet attrs)

SearchView

public SearchView(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

addHeaderView

public void addHeaderView(View headerView)

Add a header view to this SearchView, which will be placed above the search text area.

Note: due to complications with the expand/collapse animation, a header view is intended to be used with a standalone SearchView which slides up/down instead of morphing from an SearchBar.

addTransitionListener

public void addTransitionListener(
    SearchView.TransitionListener transitionListener
)

Adds a listener to handle SearchView transitions such as showing and closing.

addView

public void addView(View child, int index, LayoutParams params)

cancelBackProgress

public void cancelBackProgress()

Call this method from onBackCancelled or handleOnBackCancelled so that the back handler can cancel the back animation.

clearFocusAndHideKeyboard

public void clearFocusAndHideKeyboard()

Clears focus on the main EditText and hides the soft keyboard.

clearText

public void clearText()

Clears the text of main EditText.

getBehavior

public Behavior<SearchView> getBehavior()

getCurrentTransitionState

public SearchView.TransitionState getCurrentTransitionState()

Returns the current TransitionState for this SearchView.

getEditText

public EditText getEditText()

Returns the main EditText which can be used for hint and search text.

getHint

public CharSequence getHint()

Returns the hint of main EditText.

getSearchPrefix

public TextView getSearchPrefix()

Returns the search prefix TextView, which appears before the main EditText.

getSearchPrefixText

public CharSequence getSearchPrefixText()

Returns the search prefix text.

getSoftInputMode

public int getSoftInputMode()

Returns the current value of this SearchView's soft input mode.

getText

public Editable getText()

Returns the text of main EditText, which usually represents the search text.

getToolbar

public Toolbar getToolbar()

Returns the Toolbar used by the SearchView.

handleBackInvoked

public void handleBackInvoked()

Call this method from onBackInvoked or handleOnBackPressed so that the back handler can complete the back animation, or handle back without progress in certain cases.

hide

public void hide()

Hides the SearchView with an animation.

Note: the hide animation will not be started if the SearchView is currently hidden or hiding.

inflateMenu

public void inflateMenu(int menuResId)

Inflate a menu to provide additional options.

isAnimatedNavigationIcon

public boolean isAnimatedNavigationIcon()

Returns whether the navigation icon should be animated from the SearchBar to .

isAutoShowKeyboard

public boolean isAutoShowKeyboard()

Returns whether the soft keyboard should be shown when the SearchView is shown.

isMenuItemsAnimated

public boolean isMenuItemsAnimated()

Returns whether the menu items should be animated from the SearchBar to .

isSetupWithSearchBar

public boolean isSetupWithSearchBar()

Returns whether or not this SearchView is set up with an SearchBar.

isShowing

public boolean isShowing()

Returns whether the SearchView's main content view is shown or showing.

removeAllHeaderViews

public void removeAllHeaderViews()

Remove all header views from the section above the search text area.

removeHeaderView

public void removeHeaderView(View headerView)

Remove a header view from the section above the search text area.

removeTransitionListener

public void removeTransitionListener(
    SearchView.TransitionListener transitionListener
)

Removes a listener to handle SearchView transitions such as showing and closing.

requestFocusAndShowKeyboard

public void requestFocusAndShowKeyboard()

Requests focus on the main EditText and shows the soft keyboard.

setAnimatedNavigationIcon

public void setAnimatedNavigationIcon(boolean animatedNavigationIcon)

Sets whether the navigation icon should be animated from the SearchBar to .

setAutoShowKeyboard

public void setAutoShowKeyboard(boolean autoShowKeyboard)

Sets whether the soft keyboard should be shown when the SearchView is shown.

setElevation

public void setElevation(float elevation)

setHint

public void setHint(CharSequence hint)

Sets the hint of main EditText.

setHint

public void setHint(int hintResId)

Sets the hint of main EditText.

setMenuItemsAnimated

public void setMenuItemsAnimated(boolean menuItemsAnimated)

Sets whether the menu items should be animated from the SearchBar to .

setModalForAccessibility

public void setModalForAccessibility(boolean isSearchViewModal)

Sets whether the SearchView is modal for accessibility, i.e., whether views that are not nested within the SearchView are important for accessibility.

setOnMenuItemClickListener

public void setOnMenuItemClickListener(
    OnMenuItemClickListener onMenuItemClickListener
)

Set a listener to handle menu item clicks.

setSearchPrefixText

public void setSearchPrefixText(CharSequence searchPrefixText)

Sets the search prefix text.

setText

public void setText(CharSequence text)

Sets the text of main EditText.

setText

public void setText(int textResId)

Sets the text of main EditText.

setToolbarTouchscreenBlocksFocus

public void setToolbarTouchscreenBlocksFocus(boolean touchscreenBlocksFocus)

Sets the 'touchscreenBlocksFocus' attribute of the nested toolbar. The attribute defaults to 'true' for API level 26+. We need to set it to 'false' if keyboard navigation is needed for the search results.

setVisible

public void setVisible(boolean visible)

Updates the visibility of the SearchView without an animation.

setupWithSearchBar

public void setupWithSearchBar(SearchBar searchBar)

Sets up this SearchView with an SearchBar, which will result in the being shown when the SearchBar is clicked. This behavior will be set up automatically if the SearchBar and SearchView are in a and the SearchView is anchored to the SearchBar.

show

public void show()

Shows the SearchView with an animation.

Note: the show animation will not be started if the SearchView is currently shown or showing.

startBackProgress

public void startBackProgress(BackEventCompat backEvent)

Call this method from onBackStarted or handleOnBackStarted so that the back handler can initialize and start animating.

Note that this must be called prior to calling updateBackProgress.

updateBackProgress

public void updateBackProgress(BackEventCompat backEvent)

Call this method from onBackProgressed or handleOnBackProgressed so that the back handler can continue animating with a new progress value.

updateSoftInputMode

public void updateSoftInputMode()

Sets the soft input mode for this SearchView. This is important because the will use this to determine whether the keyboard should be shown/hidden at the same time as the expand/collapse animation, or if the keyboard should be staggered with the animation to avoid glitchiness due to a resize of the screen. This will be set automatically by the SearchView during initial render but make sure to invoke this if you are changing the soft input mode at runtime.

Protected methods

onAttachedToWindow

protected void onAttachedToWindow()

onDetachedFromWindow

protected void onDetachedFromWindow()

onFinishInflate

protected void onFinishInflate()

onRestoreInstanceState

protected void onRestoreInstanceState(Parcelable state)

onSaveInstanceState

protected Parcelable onSaveInstanceState()