SearchBar

public class SearchBar


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

Note: SearchBar does not support the setTitle and setSubtitle methods, or their corresponding xml attributes. Instead, use setHint or setText, or their corresponding xml attributes, to provide a text affordance for your .

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 abstract class SearchBar.OnLoadAnimationCallback

Callback for the animation started and stopped via startOnLoadAnimation and stopOnLoadAnimation.

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

Public fields

View
int
boolean
final TextView

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 and collapse.

void
addExpandAnimationListener(AnimatorListenerAdapter listener)

Adds a listener for the expand animation started via expand and expand.

void

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

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

Clears the text of main TextView.

boolean
collapse(View expandedView)

See collapse.

boolean
collapse(View expandedView, AppBarLayout appBarLayout)

See collapse.

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)

See expand.

boolean
expand(View expandedView, AppBarLayout appBarLayout)

See expand.

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

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

View

Returns the optional centered child view of this SearchBar

float

Returns the size in pixels of the SearchBar corners.

CharSequence

Returns the hint of main TextView.

int

Get the max width of SearchBar in pixels.

int

Returns the color of the SearchBar outline stroke.

float

Returns the width in pixels of the SearchBar outline stroke.

CharSequence

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

boolean

Whether or not the text is centered.

TextView

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

void
inflateMenu(int resId)
boolean

Returns whether the collapse animation is running.

boolean

Returns whether the default AppBarLayout scroll flags are enabled.

boolean

Returns whether the expand animation is running.

boolean

Returns whether or not the SearchBar lifts when a parent AppBarLayout lifts on scroll.

boolean

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 and collapse.

boolean
removeExpandAnimationListener(AnimatorListenerAdapter listener)

Removes a listener for the expand animation started via expand and expand.

boolean

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

Sets the hint of main TextView.

void
setHint(int hintResId)

Sets the hint of main TextView.

void
setLiftOnScroll(boolean liftOnScroll)

Sets whether the SearchBar lifts when a parent AppBarLayout lifts on scroll.

void
setMaxWidth(int maxWidth)

Sets the max width of SearchBar in pixels.

void
setNavigationIcon(Drawable navigationIcon)
void
setNavigationOnClickListener(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

SearchBar does not support the setSubtitle method, or its corresponding xml attribute.

void

Sets the text of main TextView.

void
setText(int textResId)

Sets the text of main TextView.

void
setTextCentered(boolean textCentered)

Whether or not to center the text within the TextView.

void

SearchBar does not support the setTitle method, or its corresponding xml attribute.

void

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

void

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

Protected methods

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

Public fields

centerView

public View centerView

maxWidth

public int maxWidth

textCentered

public boolean textCentered

textView

public final TextView textView

Public constructors

SearchBar

public SearchBar(Context context)

SearchBar

public SearchBar(Context context, AttributeSet attrs)

SearchBar

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

Public methods

addCollapseAnimationListener

public void addCollapseAnimationListener(AnimatorListenerAdapter listener)

Adds a listener for the collapse animation started via collapse and collapse.

addExpandAnimationListener

public void addExpandAnimationListener(AnimatorListenerAdapter listener)

Adds a listener for the expand animation started via expand and expand.

addOnLoadAnimationCallback

public void addOnLoadAnimationCallback(
    SearchBar.OnLoadAnimationCallback onLoadAnimationCallback
)

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

addView

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

clearText

public void clearText()

Clears the text of main TextView.

collapse

public boolean collapse(View expandedView)

See collapse.

collapse

public boolean collapse(View expandedView, AppBarLayout appBarLayout)

See collapse.

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.

Returns
boolean

whether or not the collapse animation was started

expand

public boolean expand(View expandedView)

See expand.

expand

public boolean expand(View expandedView, AppBarLayout appBarLayout)

See expand.

expand

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

Starts an expand animation, if it's not already started, which transitions from the 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.

Returns
boolean

whether or not the expand animation was started

getCenterView

public View getCenterView()

Returns the optional centered child view of this SearchBar

getCornerSize

public float getCornerSize()

Returns the size in pixels of the SearchBar corners.

getHint

public CharSequence getHint()

Returns the hint of main TextView.

getMaxWidth

public int getMaxWidth()

Get the max width of SearchBar in pixels.

getStrokeColor

public int getStrokeColor()

Returns the color of the SearchBar outline stroke.

getStrokeWidth

public float getStrokeWidth()

Returns the width in pixels of the SearchBar outline stroke.

getText

public CharSequence getText()

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

getTextCentered

public boolean getTextCentered()

Whether or not the text is centered.

getTextView

public TextView getTextView()

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

inflateMenu

public void inflateMenu(int resId)

isCollapsing

public boolean isCollapsing()

Returns whether the collapse animation is running.

isDefaultScrollFlagsEnabled

public boolean isDefaultScrollFlagsEnabled()

Returns whether the default AppBarLayout scroll flags are enabled. See DEFAULT_SCROLL_FLAGS.

isExpanding

public boolean isExpanding()

Returns whether the expand animation is running.

isLiftOnScroll

public boolean isLiftOnScroll()

Returns whether or not the SearchBar lifts when a parent AppBarLayout lifts on scroll.

isOnLoadAnimationFadeInEnabled

public boolean isOnLoadAnimationFadeInEnabled()

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

onInitializeAccessibilityNodeInfo

public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)

removeCollapseAnimationListener

public boolean removeCollapseAnimationListener(AnimatorListenerAdapter listener)

Removes a listener for the collapse animation started via collapse and collapse.

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 and expand.

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.

setCenterView

public void setCenterView(View view)

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

setDefaultScrollFlagsEnabled

public void setDefaultScrollFlagsEnabled(boolean defaultScrollFlagsEnabled)

Sets whether the default AppBarLayout scroll flags are enabled. See DEFAULT_SCROLL_FLAGS.

setElevation

public void setElevation(float elevation)

setHint

public void setHint(CharSequence hint)

Sets the hint of main TextView.

setHint

public void setHint(int hintResId)

Sets the hint of main TextView.

setLiftOnScroll

public void setLiftOnScroll(boolean liftOnScroll)

Sets whether the SearchBar lifts when a parent AppBarLayout lifts on scroll.

setMaxWidth

public void setMaxWidth(int maxWidth)

Sets the max width of SearchBar in pixels.

setNavigationIcon

public void setNavigationIcon(Drawable navigationIcon)

setNavigationOnClickListener

public void setNavigationOnClickListener(OnClickListener listener)

setOnLoadAnimationFadeInEnabled

public void setOnLoadAnimationFadeInEnabled(boolean onLoadAnimationFadeInEnabled)

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

setStrokeColor

public void setStrokeColor(int strokeColor)

Sets the color of the SearchBar outline stroke.

setStrokeWidth

public void setStrokeWidth(float strokeWidth)

Sets the width in pixels of the SearchBar outline stroke.

setSubtitle

public void setSubtitle(CharSequence subtitle)

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

setText

public void setText(CharSequence text)

Sets the text of main TextView.

setText

public void setText(int textResId)

Sets the text of main TextView.

setTextCentered

public void setTextCentered(boolean textCentered)

Whether or not to center the text within the TextView.

setTitle

public void setTitle(CharSequence title)

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

startOnLoadAnimation

public void startOnLoadAnimation()

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

stopOnLoadAnimation

public void stopOnLoadAnimation()

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

Protected methods

onAttachedToWindow

protected void onAttachedToWindow()

onDetachedFromWindow

protected void onDetachedFromWindow()

onLayout

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

onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

onRestoreInstanceState

protected void onRestoreInstanceState(Parcelable state)

onSaveInstanceState

protected Parcelable onSaveInstanceState()