# SearchBar

[View Source](https://github.com/material-components/material-components-android/blob/1.14.0/lib/java/com/google/android/material/search/SearchBar.java)

*** ** * ** ***


```
public class SearchBar
```

<br />

*** ** * ** ***

The `https://developer.android.com/reference/com/google/android/material/search/SearchBar` represents a floating search field with affordances for search and navigation.

Note: `https://developer.android.com/reference/com/google/android/material/search/SearchBar` does not support the `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setTitle(java.lang.CharSequence)` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setSubtitle(java.lang.CharSequence)` methods, or their corresponding xml attributes. Instead, use setHint or setText, or their corresponding xml attributes, to provide a text affordance for your `https://developer.android.com/reference/com/google/android/material/search/SearchBar`.

The example below shows how to use the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` and `https://developer.android.com/reference/com/google/android/material/search/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](https://github.com/material-components/material-components-android/blob/master/docs/components/Search.md) and [design guidelines](https://material.io/components/search/overview).

## Summary

| ### Nested types |
|---|
| `public abstract class https://developer.android.com/reference/com/google/android/material/search/SearchBar.OnLoadAnimationCallback` Callback for the animation started and stopped via `https://developer.android.com/reference/com/google/android/material/search/SearchBar#startOnLoadAnimation()` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#stopOnLoadAnimation()`. |
| `public class https://developer.android.com/reference/com/google/android/material/search/SearchBar.ScrollingViewBehavior extends https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout.ScrollingViewBehavior` Behavior that sets up the scroll-away mode for an `https://developer.android.com/reference/com/google/android/material/search/SearchBar`. |

| ### Public fields |
|---|---|
| `View` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#centerView()` |
| `int` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#endSiblingViewId()` |
| `int` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#maxWidth()` |
| `int` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#startSiblingViewId()` |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#textCentered()` |
| `final TextView` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#textView()` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/com/google/android/material/search/SearchBar#SearchBar(Context)(Context context)` |
| `https://developer.android.com/reference/com/google/android/material/search/SearchBar#SearchBar(Context,AttributeSet)(Context context, AttributeSet attrs)` |
| `https://developer.android.com/reference/com/google/android/material/search/SearchBar#SearchBar(Context,AttributeSet,int)(Context context, AttributeSet attrs, int defStyleAttr)` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#addCollapseAnimationListener(AnimatorListenerAdapter)(AnimatorListenerAdapter listener)` Adds a listener for the collapse animation started via `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View)` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View,com.google.android.material.appbar.AppBarLayout)`. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#addExpandAnimationListener(AnimatorListenerAdapter)(AnimatorListenerAdapter listener)` Adds a listener for the expand animation started via `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View)` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View,com.google.android.material.appbar.AppBarLayout)`. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#addOnLoadAnimationCallback(com.google.android.material.search.SearchBar.OnLoadAnimationCallback)( https://developer.android.com/reference/com/google/android/material/search/SearchBar.OnLoadAnimationCallback onLoadAnimationCallback )` Registers a callback for the On Load Animation, started and stopped via `https://developer.android.com/reference/com/google/android/material/search/SearchBar#startOnLoadAnimation()` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#stopOnLoadAnimation()`. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#addView(View,int,ViewGroup.LayoutParams)(View child, int index, LayoutParams params)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#clearText()()` Clears the text of main TextView. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View)(View expandedView)` See `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View,com.google.android.material.appbar.AppBarLayout,boolean)`. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View,com.google.android.material.appbar.AppBarLayout)(View expandedView, https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout appBarLayout)` See `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View,com.google.android.material.appbar.AppBarLayout,boolean)`. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View,com.google.android.material.appbar.AppBarLayout,boolean)( View expandedView, https://developer.android.com/reference/com/google/android/material/appbar/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 `https://developer.android.com/reference/com/google/android/material/search/SearchBar`. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View)(View expandedView)` See `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View,com.google.android.material.appbar.AppBarLayout,boolean)`. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View,com.google.android.material.appbar.AppBarLayout)(View expandedView, https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout appBarLayout)` See `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View,com.google.android.material.appbar.AppBarLayout,boolean)`. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View,com.google.android.material.appbar.AppBarLayout,boolean)( View expandedView, https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout appBarLayout, boolean skipAnimation )` Starts an expand animation, if it's not already started, which transitions from the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` to the `expandedView`, e.g., a contextual Toolbar. |
| `View` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#getCenterView()()` Returns the optional centered child view of this `https://developer.android.com/reference/com/google/android/material/search/SearchBar` |
| `float` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#getCornerSize()()` Returns the size in pixels of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` corners. |
| `int` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#getEndSiblingViewId()()` Returns the ID of the view to the end side of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` to be animated out of the screen during the contained expansion animation. |
| `https://developer.android.com/reference/java/lang/CharSequence` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#getHint()()` Returns the hint of main TextView. |
| `int` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#getMaxWidth()()` Get the max width of SearchBar in pixels. |
| `int` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#getStartSiblingViewId()()` Returns the ID of the view to the start side of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` to be animated out of the screen during the contained expansion animation. |
| `int` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#getStrokeColor()()` Returns the color of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` outline stroke. |
| `float` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#getStrokeWidth()()` Returns the width in pixels of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` outline stroke. |
| `https://developer.android.com/reference/java/lang/CharSequence` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#getText()()` Returns the text of main TextView, which usually represents the search text. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#getTextCentered()()` Whether or not the text is centered. |
| `TextView` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#getTextView()()` Returns the main TextView which can be used for hint and search text. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#inflateMenu(int)(int resId)` |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#isCollapsing()()` Returns whether the collapse animation is running. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#isDefaultScrollFlagsEnabled()()` Returns whether the default `https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout` scroll flags are enabled. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#isExpanding()()` Returns whether the expand animation is running. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#isLiftOnScroll()()` Returns whether or not the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` lifts when a parent `https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout` lifts on scroll. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#isOnLoadAnimationFadeInEnabled()()` Returns whether the fade in part is enabled for the on load animation. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)(AccessibilityNodeInfo info)` |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#removeCollapseAnimationListener(AnimatorListenerAdapter)(AnimatorListenerAdapter listener)` Removes a listener for the collapse animation started via `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View)` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View,com.google.android.material.appbar.AppBarLayout)`. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#removeExpandAnimationListener(AnimatorListenerAdapter)(AnimatorListenerAdapter listener)` Removes a listener for the expand animation started via `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View)` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View,com.google.android.material.appbar.AppBarLayout)`. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#removeOnLoadAnimationCallback(com.google.android.material.search.SearchBar.OnLoadAnimationCallback)( https://developer.android.com/reference/com/google/android/material/search/SearchBar.OnLoadAnimationCallback onLoadAnimationCallback )` Unregisters a callback for the On Load Animation, started and stopped via `https://developer.android.com/reference/com/google/android/material/search/SearchBar#startOnLoadAnimation()` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#stopOnLoadAnimation()`. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setCenterView(View)(View view)` Sets the center view as a child. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setDefaultScrollFlagsEnabled(boolean)(boolean defaultScrollFlagsEnabled)` Sets whether the default `https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout` scroll flags are enabled. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setElevation(float)(float elevation)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setEndSiblingViewId(int)(int endSiblingViewId)` Sets the ID of the view to the end side of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` to be animated out of the screen during the contained expansion animation. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setHint(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence hint)` Sets the hint of main TextView. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setHint(int)(int hintResId)` Sets the hint of main TextView. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setLiftOnScroll(boolean)(boolean liftOnScroll)` Sets whether the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` lifts when a parent `https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout` lifts on scroll. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setMaxWidth(int)(int maxWidth)` Sets the max width of SearchBar in pixels. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setNavigationIcon(Drawable)(Drawable navigationIcon)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setNavigationOnClickListener(OnClickListener)(OnClickListener listener)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setOnLoadAnimationFadeInEnabled(boolean)(boolean onLoadAnimationFadeInEnabled)` Sets whether the fade in part is enabled for the on load animation. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setStartSiblingViewId(int)(int startSiblingViewId)` Sets the ID of the view to the start side of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` to be animated out of the screen during the contained expansion animation. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setStrokeColor(int)(int strokeColor)` Sets the color of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` outline stroke. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setStrokeWidth(float)(float strokeWidth)` Sets the width in pixels of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` outline stroke. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setSubtitle(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence subtitle)` `https://developer.android.com/reference/com/google/android/material/search/SearchBar` does not support the setSubtitle method, or its corresponding xml attribute. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setText(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence text)` Sets the text of main TextView. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setText(int)(int textResId)` Sets the text of main TextView. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setTextCentered(boolean)(boolean textCentered)` Whether or not to center the text within the TextView. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#setTitle(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence title)` `https://developer.android.com/reference/com/google/android/material/search/SearchBar` does not support the setTitle method, or its corresponding xml attribute. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#startOnLoadAnimation()()` Starts the on load animation which transitions from the center view to the hint TextView. |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#stopOnLoadAnimation()()` Stops the on load animation which transitions from the center view to the hint TextView. |

| ### Protected methods |
|---|---|
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#onAttachedToWindow()()` |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#onDetachedFromWindow()()` |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#onLayout(boolean,int,int,int,int)(boolean changed, int left, int top, int right, int bottom)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#onMeasure(int,int)(int widthMeasureSpec, int heightMeasureSpec)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#onRestoreInstanceState(Parcelable)(Parcelable state)` |
| `Parcelable` | `https://developer.android.com/reference/com/google/android/material/search/SearchBar#onSaveInstanceState()()` |

## Public fields

### centerView

```
public View centerView
```

### endSiblingViewId

```
public int endSiblingViewId
```

### maxWidth

```
public int maxWidth
```

### startSiblingViewId

```
public int startSiblingViewId
```

### 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 `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View)` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View,com.google.android.material.appbar.AppBarLayout)`.

### addExpandAnimationListener

```
public void addExpandAnimationListener(AnimatorListenerAdapter listener)
```

Adds a listener for the expand animation started via `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View)` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View,com.google.android.material.appbar.AppBarLayout)`.

### addOnLoadAnimationCallback

```
public void addOnLoadAnimationCallback(
    SearchBar.OnLoadAnimationCallback onLoadAnimationCallback
)
```

Registers a callback for the On Load Animation, started and stopped via `https://developer.android.com/reference/com/google/android/material/search/SearchBar#startOnLoadAnimation()` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#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 `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View,com.google.android.material.appbar.AppBarLayout,boolean)`.

### collapse

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

See `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View,com.google.android.material.appbar.AppBarLayout,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 `https://developer.android.com/reference/com/google/android/material/search/SearchBar`.

Note: If you are using an `https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout` in conjunction with the `https://developer.android.com/reference/com/google/android/material/search/SearchBar`, you may pass in a reference to your `https://developer.android.com/reference/com/google/android/material/appbar/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 `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View,com.google.android.material.appbar.AppBarLayout,boolean)`.

### expand

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

See `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View,com.google.android.material.appbar.AppBarLayout,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 `https://developer.android.com/reference/com/google/android/material/search/SearchBar` to the `expandedView`, e.g., a contextual Toolbar.

Note: If you are using an `https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout` in conjunction with the `https://developer.android.com/reference/com/google/android/material/search/SearchBar`, you may pass in a reference to your `https://developer.android.com/reference/com/google/android/material/appbar/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 `https://developer.android.com/reference/com/google/android/material/search/SearchBar`

### getCornerSize

```
public float getCornerSize()
```

Returns the size in pixels of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` corners.

### getEndSiblingViewId

```
public int getEndSiblingViewId()
```

Returns the ID of the view to the end side of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` to be animated out of the screen during the contained expansion animation.

### getHint

```
public CharSequence getHint()
```

Returns the hint of main TextView.

### getMaxWidth

```
public int getMaxWidth()
```

Get the max width of SearchBar in pixels.

### getStartSiblingViewId

```
public int getStartSiblingViewId()
```

Returns the ID of the view to the start side of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` to be animated out of the screen during the contained expansion animation.

### getStrokeColor

```
public int getStrokeColor()
```

Returns the color of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` outline stroke.

### getStrokeWidth

```
public float getStrokeWidth()
```

Returns the width in pixels of the `https://developer.android.com/reference/com/google/android/material/search/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 `https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout` scroll flags are enabled. See `https://developer.android.com/reference/com/google/android/material/search/SearchBar#DEFAULT_SCROLL_FLAGS()`.

### isExpanding

```
public boolean isExpanding()
```

Returns whether the expand animation is running.

### isLiftOnScroll

```
public boolean isLiftOnScroll()
```

Returns whether or not the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` lifts when a parent `https://developer.android.com/reference/com/google/android/material/appbar/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 `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View)` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#collapse(View,com.google.android.material.appbar.AppBarLayout)`.

| 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 `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View)` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#expand(View,com.google.android.material.appbar.AppBarLayout)`.

| 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 `https://developer.android.com/reference/com/google/android/material/search/SearchBar#startOnLoadAnimation()` and `https://developer.android.com/reference/com/google/android/material/search/SearchBar#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 `https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout` scroll flags are enabled. See `https://developer.android.com/reference/com/google/android/material/search/SearchBar#DEFAULT_SCROLL_FLAGS()`.

### setElevation

```
public void setElevation(float elevation)
```

### setEndSiblingViewId

```
public void setEndSiblingViewId(int endSiblingViewId)
```

Sets the ID of the view to the end side of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` to be animated out of the screen during the contained expansion animation.

### 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 `https://developer.android.com/reference/com/google/android/material/search/SearchBar` lifts when a parent `https://developer.android.com/reference/com/google/android/material/appbar/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.

### setStartSiblingViewId

```
public void setStartSiblingViewId(int startSiblingViewId)
```

Sets the ID of the view to the start side of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` to be animated out of the screen during the contained expansion animation.

### setStrokeColor

```
public void setStrokeColor(int strokeColor)
```

Sets the color of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` outline stroke.

### setStrokeWidth

```
public void setStrokeWidth(float strokeWidth)
```

Sets the width in pixels of the `https://developer.android.com/reference/com/google/android/material/search/SearchBar` outline stroke.

### setSubtitle

```
public void setSubtitle(CharSequence subtitle)
```

`https://developer.android.com/reference/com/google/android/material/search/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 `https://developer.android.com/reference/com/google/android/material/search/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)
```

`https://developer.android.com/reference/com/google/android/material/search/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 `https://developer.android.com/reference/com/google/android/material/search/SearchBar`.

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

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