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.

Constructors

Link copied to clipboard
constructor(@NonNull context: Context)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet, defStyleAttr: Int)

Types

Link copied to clipboard
Behavior that sets up an SearchView with an SearchBar.
Link copied to clipboard
Callback interface that provides important transition events for a SearchView.
Link copied to clipboard
Enum that defines the possible transition states of an SearchView.

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open val softInputMode: Int

Functions

Link copied to clipboard
open fun addHeaderView(@NonNull headerView: View)
Add a header view to this SearchView, which will be placed above the search text area.
Link copied to clipboard
Adds a listener to handle SearchView transitions such as showing and closing.
Link copied to clipboard
open fun addView(child: View, index: Int, params: ViewGroup.LayoutParams)
Link copied to clipboard
Call this method from onBackCancelled or handleOnBackCancelled so that the back handler can cancel the back animation.
Link copied to clipboard
Clears focus on the main EditText and hides the soft keyboard.
Link copied to clipboard
open fun clearText()
Clears the text of main EditText.
Link copied to clipboard
Link copied to clipboard
Returns the hint of main EditText.
Link copied to clipboard
Returns the search prefix text.
Link copied to clipboard
Returns the text of main EditText, which usually represents the search text.
Link copied to clipboard
Returns the Toolbar used by the SearchView.
Link copied to clipboard
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.
Link copied to clipboard
open fun hide()
Hides the SearchView with an animation.
Link copied to clipboard
open fun inflateMenu(@MenuRes menuResId: Int)
Inflate a menu to provide additional options.
Link copied to clipboard
Returns whether the navigation icon should be animated from the SearchBar to .
Link copied to clipboard
Returns whether the soft keyboard should be shown when the SearchView is shown.
Link copied to clipboard
Returns whether the menu items should be animated from the SearchBar to .
Link copied to clipboard
Returns whether or not this SearchView is set up with an SearchBar.
Link copied to clipboard
open fun isShowing(): Boolean
Returns whether the SearchView's main content view is shown or showing.
Link copied to clipboard
Returns whether the soft keyboard should be shown with WindowInsetsController.
Link copied to clipboard
Remove all header views from the section above the search text area.
Link copied to clipboard
open fun removeHeaderView(@NonNull headerView: View)
Remove a header view from the section above the search text area.
Link copied to clipboard
Removes a listener to handle SearchView transitions such as showing and closing.
Link copied to clipboard
Requests focus on the main EditText and shows the soft keyboard.
Link copied to clipboard
open fun setAnimatedNavigationIcon(animatedNavigationIcon: Boolean)
Sets whether the navigation icon should be animated from the SearchBar to .
Link copied to clipboard
open fun setAutoShowKeyboard(autoShowKeyboard: Boolean)
Sets whether the soft keyboard should be shown when the SearchView is shown.
Link copied to clipboard
Link copied to clipboard
open fun setHint(@StringRes hintResId: Int)
open fun setHint(@Nullable hint: CharSequence)
Sets the hint of main EditText.
Link copied to clipboard
open fun setMenuItemsAnimated(menuItemsAnimated: Boolean)
Sets whether the menu items should be animated from the SearchBar to .
Link copied to clipboard
open fun setModalForAccessibility(isSearchViewModal: Boolean)
Sets whether the SearchView is modal for accessibility, i.e.
Link copied to clipboard
Set a listener to handle menu item clicks.
Link copied to clipboard
open fun setSearchPrefixText(@Nullable searchPrefixText: CharSequence)
Sets the search prefix text.
Link copied to clipboard
Enables/disables the status bar spacer, which can be used in cases where the status bar is translucent and the SearchView should not overlap the status bar area.
Link copied to clipboard
open fun setText(@StringRes textResId: Int)
open fun setText(@Nullable text: CharSequence)
Sets the text of main EditText.
Link copied to clipboard
open fun setToolbarTouchscreenBlocksFocus(touchscreenBlocksFocus: Boolean)
Sets the 'touchscreenBlocksFocus' attribute of the nested toolbar.
Link copied to clipboard
open fun setupWithSearchBar(@Nullable searchBar: SearchBar)
Sets up this SearchView with an SearchBar, which will result in the being shown when the SearchBar is clicked.
Link copied to clipboard
open fun setUseWindowInsetsController(useWindowInsetsController: Boolean)
Sets whether the soft keyboard should be shown with WindowInsetsController.
Link copied to clipboard
open fun setVisible(visible: Boolean)
Updates the visibility of the SearchView without an animation.
Link copied to clipboard
open fun show()
Shows the SearchView with an animation.
Link copied to clipboard
Call this method from onBackStarted or handleOnBackStarted so that the back handler can initialize and start animating.
Link copied to clipboard
Call this method from onBackProgressed or handleOnBackProgressed so that the back handler can continue animating with a new progress value.
Link copied to clipboard
Sets the soft input mode for this SearchView.