SearchView

public class SearchView
extends FrameLayout implements CoordinatorLayout.AttachedBehavior

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ com.google.android.material.search.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 classes

class SearchView.Behavior

Behavior that sets up an SearchView with an SearchBar

interface SearchView.TransitionListener

Callback interface that provides important transition events for a SearchView

enum SearchView.TransitionState

Enum that defines the possible transition states of an SearchView

Inherited constants

Inherited fields

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 addTransitionListener(SearchView.TransitionListener transitionListener)

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

void addView(View child, int index, ViewGroup.LayoutParams params)
void cancelBackProgress()
void clearFocusAndHideKeyboard()

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

void clearText()

Clears the text of main EditText.

Behavior<SearchView> getBehavior()
SearchView.TransitionState getCurrentTransitionState()

Returns the current SearchView.TransitionState for this SearchView.

EditText getEditText()

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

CharSequence getHint()

Returns the hint of main EditText.

TextView getSearchPrefix()

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

CharSequence getSearchPrefixText()

Returns the search prefix text.

int getSoftInputMode()

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

Editable getText()

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

Toolbar getToolbar()

Returns the Toolbar used by the SearchView.

void handleBackInvoked()
void hide()

Hides the SearchView with an animation.

void inflateMenu(int menuResId)

Inflate a menu to provide additional options.

boolean isAnimatedNavigationIcon()

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

boolean isAutoShowKeyboard()

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

boolean isMenuItemsAnimated()

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

boolean isSetupWithSearchBar()

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

boolean isShowing()

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

void removeAllHeaderViews()

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 removeTransitionListener(SearchView.TransitionListener transitionListener)

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

void requestFocusAndShowKeyboard()

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 SearchView.

void setAutoShowKeyboard(boolean autoShowKeyboard)

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

void setElevation(float elevation)
void setHint(CharSequence hint)

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 SearchView.

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(Toolbar.OnMenuItemClickListener onMenuItemClickListener)

Set a listener to handle menu item clicks.

void setSearchPrefixText(CharSequence searchPrefixText)

Sets the search prefix text.

void setText(int textResId)

Sets the text of main EditText.

void setText(CharSequence text)

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 setupWithSearchBar(SearchBar searchBar)

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

void show()

Shows the SearchView with an animation.

void startBackProgress(BackEventCompat backEvent)
void updateBackProgress(BackEventCompat backEvent)
void updateSoftInputMode()

Sets the soft input mode for this SearchView.

Protected methods

void onAttachedToWindow()
void onFinishInflate()
void onRestoreInstanceState(Parcelable state)
Parcelable onSaveInstanceState()

Inherited methods

Public constructors

SearchView

public SearchView (Context context)

Parameters
context Context

SearchView

public SearchView (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

SearchView

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

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.

Parameters
headerView View

addTransitionListener

public void addTransitionListener (SearchView.TransitionListener transitionListener)

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

Parameters
transitionListener SearchView.TransitionListener

addView

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

Parameters
child View

index int

params ViewGroup.LayoutParams

cancelBackProgress

public void cancelBackProgress ()

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 ()

Returns
Behavior<SearchView>

getCurrentTransitionState

public SearchView.TransitionState getCurrentTransitionState ()

Returns the current SearchView.TransitionState for this SearchView.

Returns
SearchView.TransitionState

getEditText

public EditText getEditText ()

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

Returns
EditText

getHint

public CharSequence getHint ()

Returns the hint of main EditText.

Returns
CharSequence

getSearchPrefix

public TextView getSearchPrefix ()

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

Returns
TextView

getSearchPrefixText

public CharSequence getSearchPrefixText ()

Returns the search prefix text.

Returns
CharSequence

getSoftInputMode

public int getSoftInputMode ()

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

Returns
int

getText

public Editable getText ()

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

Returns
Editable

getToolbar

public Toolbar getToolbar ()

Returns the Toolbar used by the SearchView.

Returns
Toolbar

handleBackInvoked

public void handleBackInvoked ()

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.

Parameters
menuResId int

isAnimatedNavigationIcon

public boolean isAnimatedNavigationIcon ()

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

Returns
boolean

isAutoShowKeyboard

public boolean isAutoShowKeyboard ()

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

Returns
boolean

isMenuItemsAnimated

public boolean isMenuItemsAnimated ()

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

Returns
boolean

isSetupWithSearchBar

public boolean isSetupWithSearchBar ()

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

Returns
boolean

isShowing

public boolean isShowing ()

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

Returns
boolean

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.

Parameters
headerView View

removeTransitionListener

public void removeTransitionListener (SearchView.TransitionListener transitionListener)

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

Parameters
transitionListener SearchView.TransitionListener

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 SearchView.

Parameters
animatedNavigationIcon boolean

setAutoShowKeyboard

public void setAutoShowKeyboard (boolean autoShowKeyboard)

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

Parameters
autoShowKeyboard boolean

setElevation

public void setElevation (float elevation)

Parameters
elevation float

setHint

public void setHint (CharSequence hint)

Sets the hint of main EditText.

Parameters
hint CharSequence

setHint

public void setHint (int hintResId)

Sets the hint of main EditText.

Parameters
hintResId int

setMenuItemsAnimated

public void setMenuItemsAnimated (boolean menuItemsAnimated)

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

Parameters
menuItemsAnimated boolean

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.

Parameters
isSearchViewModal boolean

setOnMenuItemClickListener

public void setOnMenuItemClickListener (Toolbar.OnMenuItemClickListener onMenuItemClickListener)

Set a listener to handle menu item clicks.

Parameters
onMenuItemClickListener Toolbar.OnMenuItemClickListener

setSearchPrefixText

public void setSearchPrefixText (CharSequence searchPrefixText)

Sets the search prefix text.

Parameters
searchPrefixText CharSequence

setText

public void setText (int textResId)

Sets the text of main EditText.

Parameters
textResId int

setText

public void setText (CharSequence text)

Sets the text of main EditText.

Parameters
text CharSequence

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.

Parameters
touchscreenBlocksFocus boolean

setVisible

public void setVisible (boolean visible)

Updates the visibility of the SearchView without an animation.

Parameters
visible boolean

setupWithSearchBar

public void setupWithSearchBar (SearchBar searchBar)

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

Parameters
searchBar 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)

Parameters
backEvent BackEventCompat

updateBackProgress

public void updateBackProgress (BackEventCompat backEvent)

Parameters
backEvent BackEventCompat

updateSoftInputMode

public void updateSoftInputMode ()

Sets the soft input mode for this SearchView. This is important because the SearchView 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 ()

onFinishInflate

protected void onFinishInflate ()

onRestoreInstanceState

protected void onRestoreInstanceState (Parcelable state)

Parameters
state Parcelable

onSaveInstanceState

protected Parcelable onSaveInstanceState ()

Returns
Parcelable