SearchBar

open class SearchBar : Toolbar

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.

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
Callback for the animation started and stopped via startOnLoadAnimation and stopOnLoadAnimation.
Link copied to clipboard
Behavior that sets up the scroll-away mode for an SearchBar.

Properties

Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
Adds a listener for the collapse animation started via collapse and collapse.
Link copied to clipboard
Adds a listener for the expand animation started via expand and expand.
Link copied to clipboard
Registers a callback for the On Load Animation, started and stopped via startOnLoadAnimation and stopOnLoadAnimation.
Link copied to clipboard
open fun addView(child: View, index: Int, params: ViewGroup.LayoutParams)
Link copied to clipboard
open fun clearText()
Clears the text of main TextView.
Link copied to clipboard
@CanIgnoreReturnValue
open fun collapse(@NonNull expandedView: View): Boolean
@CanIgnoreReturnValue
open fun collapse(@NonNull expandedView: View, @Nullable appBarLayout: AppBarLayout): Boolean
@CanIgnoreReturnValue
open fun collapse(@NonNull expandedView: View, @Nullable appBarLayout: AppBarLayout, skipAnimation: Boolean): Boolean
Starts a collapse animation, if it's not already started, which transitions from the expandedView, e.g.
Link copied to clipboard
@CanIgnoreReturnValue
open fun expand(@NonNull expandedView: View): Boolean
@CanIgnoreReturnValue
open fun expand(@NonNull expandedView: View, @Nullable appBarLayout: AppBarLayout): Boolean
See expand.
@CanIgnoreReturnValue
open fun expand(@NonNull expandedView: View, @Nullable appBarLayout: AppBarLayout, skipAnimation: Boolean): Boolean
Starts an expand animation, if it's not already started, which transitions from the to the expandedView, e.g.
Link copied to clipboard
open fun getCornerSize(): Float
Returns the size in pixels of the SearchBar corners.
Link copied to clipboard
Returns the hint of main TextView.
Link copied to clipboard
Returns the color of the SearchBar outline stroke.
Link copied to clipboard
Returns the width in pixels of the SearchBar outline stroke.
Link copied to clipboard
Returns the text of main TextView, which usually represents the search text.
Link copied to clipboard
open fun inflateMenu(@MenuRes resId: Int)
Link copied to clipboard
open fun isCollapsing(): Boolean
Returns whether the collapse animation is running.
Link copied to clipboard
Returns whether the default AppBarLayout scroll flags are enabled.
Link copied to clipboard
open fun isExpanding(): Boolean
Returns whether the expand animation is running.
Link copied to clipboard
Returns whether the fade in part is enabled for the on load animation.
Link copied to clipboard
Removes a listener for the collapse animation started via collapse and collapse.
Link copied to clipboard
Removes a listener for the expand animation started via expand and expand.
Link copied to clipboard
Unregisters a callback for the On Load Animation, started and stopped via startOnLoadAnimation and stopOnLoadAnimation.
Link copied to clipboard
open fun setDefaultScrollFlagsEnabled(defaultScrollFlagsEnabled: Boolean)
Sets whether the default AppBarLayout scroll flags are enabled.
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 TextView.
Link copied to clipboard
open fun setNavigationIcon(@Nullable navigationIcon: Drawable)
Link copied to clipboard
open fun setOnLoadAnimationFadeInEnabled(onLoadAnimationFadeInEnabled: Boolean)
Sets whether the fade in part is enabled for the on load animation.
Link copied to clipboard
open fun setStrokeColor(@ColorInt strokeColor: Int)
Sets the color of the SearchBar outline stroke.
Link copied to clipboard
open fun setStrokeWidth(@Dimension strokeWidth: Float)
Sets the width in pixels of the SearchBar outline stroke.
Link copied to clipboard
open fun setSubtitle(subtitle: CharSequence)
SearchBar does not support the setSubtitle method, or its corresponding xml attribute.
Link copied to clipboard
open fun setText(@StringRes textResId: Int)
open fun setText(@Nullable text: CharSequence)
Sets the text of main TextView.
Link copied to clipboard
open fun setTitle(title: CharSequence)
SearchBar does not support the setTitle method, or its corresponding xml attribute.
Link copied to clipboard
Starts the on load animation which transitions from the center view to the hint .
Link copied to clipboard
Stops the on load animation which transitions from the center view to the hint .