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

Public constructors

SearchView(Context context)

SearchView(Context context, AttributeSet attrs)

SearchView(Context context, AttributeSet attrs, int defStyleAttr)

Public fields

Public constructors

SearchView

public SearchView(Context context, AttributeSet attrs)

SearchView

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

Public methods

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.

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.

getEditText

public EditText getEditText()

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

getSearchPrefix

public TextView getSearchPrefix()

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

getText

public Editable getText()

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

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.

public void inflateMenu(int menuResId)

Inflate a menu to provide additional options.

isShowing

public boolean isShowing()

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

public void removeAllHeaderViews()

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

public void removeHeaderView(View headerView)

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

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.

setHint

public void setHint(int hintResId)

Sets the hint of main EditText.

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.

public void setOnMenuItemClickListener(
    OnMenuItemClickListener onMenuItemClickListener
)

Set a listener to handle menu item clicks.

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.

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

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2025-10-28 UTC.