SearchBar

public class SearchBar
extends Toolbar

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ androidx.appcompat.widget.Toolbar
         ↳ com.google.android.material.search.SearchBar


The SearchBar represents a floating search field with affordances for search and navigation.

Note: SearchBar does not support the Toolbar.setTitle(int) and Toolbar.setSubtitle(int) methods, or their corresponding xml attributes. Instead, use setHint(int) or setText(int), or their corresponding xml attributes, to provide a text affordance for your 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 SearchBar.OnLoadAnimationCallback

Callback for the animation started and stopped via SearchBar.startOnLoadAnimation() and SearchBar.stopOnLoadAnimation()

class SearchBar.ScrollingViewBehavior

Behavior that sets up the scroll-away mode for an SearchBar

Inherited constants

Inherited fields

Public constructors

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

Public methods

void addCollapseAnimationListener(AnimatorListenerAdapter listener)

Adds a listener for the collapse animation started via collapse(View) and collapse(View, AppBarLayout).

void addExpandAnimationListener(AnimatorListenerAdapter listener)

Adds a listener for the expand animation started via expand(View) and expand(View, AppBarLayout).

void addOnLoadAnimationCallback(SearchBar.OnLoadAnimationCallback onLoadAnimationCallback)

Registers a callback for the On Load Animation, started and stopped via startOnLoadAnimation() and stopOnLoadAnimation().

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

Clears the text of main TextView.

boolean collapse(View expandedView)

See collapse(View, AppBarLayout, boolean).

boolean collapse(View expandedView, AppBarLayout appBarLayout)

See collapse(View, AppBarLayout, boolean).

boolean collapse(View expandedView, AppBarLayout appBarLayout, boolean skipAnimation)

Starts a collapse animation, if it's not already started, which transitions from the expandedView, e.g., a contextual Toolbar, to the SearchBar.

boolean expand(View expandedView, AppBarLayout appBarLayout)

See expand(View, AppBarLayout, boolean).

boolean expand(View expandedView)

See expand(View, AppBarLayout, boolean).

boolean expand(View expandedView, AppBarLayout appBarLayout, boolean skipAnimation)

Starts an expand animation, if it's not already started, which transitions from the SearchBar to the expandedView, e.g., a contextual Toolbar.

View getCenterView()

Returns the optional centered child view of this SearchBar

float getCornerSize()

Returns the size in pixels of the SearchBar corners.

CharSequence getHint()

Returns the hint of main TextView.

int getStrokeColor()

Returns the color of the SearchBar outline stroke.

float getStrokeWidth()

Returns the width in pixels of the SearchBar outline stroke.

CharSequence getText()

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

TextView getTextView()

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

void inflateMenu(int resId)
boolean isCollapsing()

Returns whether the collapse animation is running.

boolean isDefaultScrollFlagsEnabled()

Returns whether the default AppBarLayout scroll flags are enabled.

boolean isExpanding()

Returns whether the expand animation is running.

boolean isOnLoadAnimationFadeInEnabled()

Returns whether the fade in part is enabled for the on load animation.

void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
boolean removeCollapseAnimationListener(AnimatorListenerAdapter listener)

Removes a listener for the collapse animation started via collapse(View) and collapse(View, AppBarLayout).

boolean removeExpandAnimationListener(AnimatorListenerAdapter listener)

Removes a listener for the expand animation started via expand(View) and expand(View, AppBarLayout).

boolean removeOnLoadAnimationCallback(SearchBar.OnLoadAnimationCallback onLoadAnimationCallback)

Unregisters a callback for the On Load Animation, started and stopped via startOnLoadAnimation() and stopOnLoadAnimation().

void setCenterView(View view)

Sets the center view as a child.

void setDefaultScrollFlagsEnabled(boolean defaultScrollFlagsEnabled)

Sets whether the default AppBarLayout scroll flags are enabled.

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

Sets the hint of main TextView.

void setHint(int hintResId)

Sets the hint of main TextView.

void setNavigationIcon(Drawable navigationIcon)
void setNavigationOnClickListener(View.OnClickListener listener)
void setOnLoadAnimationFadeInEnabled(boolean onLoadAnimationFadeInEnabled)

Sets whether the fade in part is enabled for the on load animation.

void setStrokeColor(int strokeColor)

Sets the color of the SearchBar outline stroke.

void setStrokeWidth(float strokeWidth)

Sets the width in pixels of the SearchBar outline stroke.

void setSubtitle(CharSequence subtitle)

SearchBar does not support the Toolbar.setSubtitle(int) method, or its corresponding xml attribute.

void setText(int textResId)

Sets the text of main TextView.

void setText(CharSequence text)

Sets the text of main TextView.

void setTitle(CharSequence title)

SearchBar does not support the Toolbar.setTitle(int) method, or its corresponding xml attribute.

void startOnLoadAnimation()

Starts the on load animation which transitions from the center view to the hint TextView.

void stopOnLoadAnimation()

Stops the on load animation which transitions from the center view to the hint TextView.

Protected methods

void onAttachedToWindow()
void onLayout(boolean changed, int left, int top, int right, int bottom)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void onRestoreInstanceState(Parcelable state)
Parcelable onSaveInstanceState()

Inherited methods

Public constructors

SearchBar

public SearchBar (Context context)

Parameters
context Context

SearchBar

public SearchBar (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

SearchBar

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

addCollapseAnimationListener

public void addCollapseAnimationListener (AnimatorListenerAdapter listener)

Adds a listener for the collapse animation started via collapse(View) and collapse(View, AppBarLayout).

Parameters
listener AnimatorListenerAdapter

addExpandAnimationListener

public void addExpandAnimationListener (AnimatorListenerAdapter listener)

Adds a listener for the expand animation started via expand(View) and expand(View, AppBarLayout).

Parameters
listener AnimatorListenerAdapter

addOnLoadAnimationCallback

public void addOnLoadAnimationCallback (SearchBar.OnLoadAnimationCallback onLoadAnimationCallback)

Registers a callback for the On Load Animation, started and stopped via startOnLoadAnimation() and stopOnLoadAnimation().

Parameters
onLoadAnimationCallback SearchBar.OnLoadAnimationCallback

addView

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

Parameters
child View

index int

params ViewGroup.LayoutParams

clearText

public void clearText ()

Clears the text of main TextView.

collapse

public boolean collapse (View expandedView)

See collapse(View, AppBarLayout, boolean).

Parameters
expandedView View

Returns
boolean

collapse

public boolean collapse (View expandedView, 
                AppBarLayout appBarLayout)

See collapse(View, AppBarLayout, boolean).

Parameters
expandedView View

appBarLayout AppBarLayout

Returns
boolean

collapse

public boolean collapse (View expandedView, 
                AppBarLayout appBarLayout, 
                boolean skipAnimation)

Starts a collapse animation, if it's not already started, which transitions from the expandedView, e.g., a contextual Toolbar, to the SearchBar.

Note: If you are using an AppBarLayout in conjunction with the SearchBar, you may pass in a reference to your AppBarLayout so that its visibility and offset can be taken into account for the animation.

Parameters
expandedView View

appBarLayout AppBarLayout

skipAnimation boolean

Returns
boolean whether or not the collapse animation was started

expand

public boolean expand (View expandedView, 
                AppBarLayout appBarLayout)

See expand(View, AppBarLayout, boolean).

Parameters
expandedView View

appBarLayout AppBarLayout

Returns
boolean

expand

public boolean expand (View expandedView)

See expand(View, AppBarLayout, boolean).

Parameters
expandedView View

Returns
boolean

expand

public boolean expand (View expandedView, 
                AppBarLayout appBarLayout, 
                boolean skipAnimation)

Starts an expand animation, if it's not already started, which transitions from the SearchBar to the expandedView, e.g., a contextual Toolbar.

Note: If you are using an AppBarLayout in conjunction with the SearchBar, you may pass in a reference to your AppBarLayout so that its visibility and offset can be taken into account for the animation.

Parameters
expandedView View

appBarLayout AppBarLayout

skipAnimation boolean

Returns
boolean whether or not the expand animation was started

getCenterView

public View getCenterView ()

Returns the optional centered child view of this SearchBar

Returns
View

getCornerSize

public float getCornerSize ()

Returns the size in pixels of the SearchBar corners.

Returns
float

getHint

public CharSequence getHint ()

Returns the hint of main TextView.

Returns
CharSequence

getStrokeColor

public int getStrokeColor ()

Returns the color of the SearchBar outline stroke.

Returns
int

getStrokeWidth

public float getStrokeWidth ()

Returns the width in pixels of the SearchBar outline stroke.

Returns
float

getText

public CharSequence getText ()

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

Returns
CharSequence

getTextView

public TextView getTextView ()

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

Returns
TextView

inflateMenu

public void inflateMenu (int resId)

Parameters
resId int

isCollapsing

public boolean isCollapsing ()

Returns whether the collapse animation is running.

Returns
boolean

isDefaultScrollFlagsEnabled

public boolean isDefaultScrollFlagsEnabled ()

Returns whether the default AppBarLayout scroll flags are enabled. See ERROR(/SearchBar#DEFAULT_SCROLL_FLAGS).

Returns
boolean

isExpanding

public boolean isExpanding ()

Returns whether the expand animation is running.

Returns
boolean

isOnLoadAnimationFadeInEnabled

public boolean isOnLoadAnimationFadeInEnabled ()

Returns whether the fade in part is enabled for the on load animation.

Returns
boolean

onInitializeAccessibilityNodeInfo

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Parameters
info AccessibilityNodeInfo

removeCollapseAnimationListener

public boolean removeCollapseAnimationListener (AnimatorListenerAdapter listener)

Removes a listener for the collapse animation started via collapse(View) and collapse(View, AppBarLayout).

Parameters
listener AnimatorListenerAdapter

Returns
boolean true if a listener was removed as a result of this call

removeExpandAnimationListener

public boolean removeExpandAnimationListener (AnimatorListenerAdapter listener)

Removes a listener for the expand animation started via expand(View) and expand(View, AppBarLayout).

Parameters
listener AnimatorListenerAdapter

Returns
boolean true if a listener was removed as a result of this call

removeOnLoadAnimationCallback

public boolean removeOnLoadAnimationCallback (SearchBar.OnLoadAnimationCallback onLoadAnimationCallback)

Unregisters a callback for the On Load Animation, started and stopped via startOnLoadAnimation() and stopOnLoadAnimation().

Parameters
onLoadAnimationCallback SearchBar.OnLoadAnimationCallback

Returns
boolean

setCenterView

public void setCenterView (View view)

Sets the center view as a child. Pass in null for view to remove the center view.

Parameters
view View

setDefaultScrollFlagsEnabled

public void setDefaultScrollFlagsEnabled (boolean defaultScrollFlagsEnabled)

Sets whether the default AppBarLayout scroll flags are enabled. See ERROR(/SearchBar#DEFAULT_SCROLL_FLAGS).

Parameters
defaultScrollFlagsEnabled boolean

setElevation

public void setElevation (float elevation)

Parameters
elevation float

setHint

public void setHint (CharSequence hint)

Sets the hint of main TextView.

Parameters
hint CharSequence

setHint

public void setHint (int hintResId)

Sets the hint of main TextView.

Parameters
hintResId int

setNavigationIcon

public void setNavigationIcon (Drawable navigationIcon)

Parameters
navigationIcon Drawable

setNavigationOnClickListener

public void setNavigationOnClickListener (View.OnClickListener listener)

Parameters
listener View.OnClickListener

setOnLoadAnimationFadeInEnabled

public void setOnLoadAnimationFadeInEnabled (boolean onLoadAnimationFadeInEnabled)

Sets whether the fade in part is enabled for the on load animation.

Parameters
onLoadAnimationFadeInEnabled boolean

setStrokeColor

public void setStrokeColor (int strokeColor)

Sets the color of the SearchBar outline stroke.

Parameters
strokeColor int

setStrokeWidth

public void setStrokeWidth (float strokeWidth)

Sets the width in pixels of the SearchBar outline stroke.

Parameters
strokeWidth float

setSubtitle

public void setSubtitle (CharSequence subtitle)

SearchBar does not support the Toolbar.setSubtitle(int) method, or its corresponding xml attribute. Instead, use setHint(int) or setText(int), or their corresponding xml attributes, to provide a text affordance for your SearchBar.

Parameters
subtitle CharSequence

setText

public void setText (int textResId)

Sets the text of main TextView.

Parameters
textResId int

setText

public void setText (CharSequence text)

Sets the text of main TextView.

Parameters
text CharSequence

setTitle

public void setTitle (CharSequence title)

SearchBar does not support the Toolbar.setTitle(int) method, or its corresponding xml attribute. Instead, use setHint(int) or setText(int), or their corresponding xml attributes, to provide a text affordance for your SearchBar.

Parameters
title CharSequence

startOnLoadAnimation

public void startOnLoadAnimation ()

Starts the on load animation which transitions from the center view to the hint TextView.

stopOnLoadAnimation

public void stopOnLoadAnimation ()

Stops the on load animation which transitions from the center view to the hint TextView.

Protected methods

onAttachedToWindow

protected void onAttachedToWindow ()

onLayout

protected void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

Parameters
changed boolean

left int

top int

right int

bottom int

onMeasure

protected void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onRestoreInstanceState

protected void onRestoreInstanceState (Parcelable state)

Parameters
state Parcelable

onSaveInstanceState

protected Parcelable onSaveInstanceState ()

Returns
Parcelable