Skip to content

Commit

Permalink
[Docs][Search] Update doc with more info on SearchView EditText and n…
Browse files Browse the repository at this point in the history
…ested content

Resolves #4189

PiperOrigin-RevId: 636998698
  • Loading branch information
dsn5ft committed May 25, 2024
1 parent 2bc1a2f commit cb5dc7f
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion docs/components/Search.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ searchView.setOnMenuItemClickListener(
});
```

Additionally, here is an example of how you can carry over the search text to
Additionally, `SearchView` exposes its main `EditText` via a `getEditText()`
method, so you can use any of the traditional
[EditText APIs](https://developer.android.com/reference/android/widget/EditText)
to configure the search field (`setText()`, `addTextChangedListener()`, etc.).

Here is an example of how you can carry over the search text to
the `SearchBar`, as well as hide the `SearchView` when the user finishes typing
and presses enter:

Expand Down Expand Up @@ -317,6 +322,27 @@ Additionally, with this pattern it is common to hide the back button to reduce
clutter, as navigation can be handled outside of the search view. This can be
accomplished by setting `app:hideNavigationIcon="true"` on your `SearchView`.

## Search History, Suggestions, and Results

`SearchView` is a view group component, meaning you can nest content inside of
it such as:

- Search history when the `SearchView` is first expanded
- Search suggestions when the user is typing
- Search results once the user submits the search

```xml
<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>
```

## Putting it all together

Putting it all together and using the scroll-away mode, the `SearchBar` and
Expand Down Expand Up @@ -350,7 +376,9 @@ Putting it all together and using the scroll-away mode, the `SearchBar` and
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>
```
Expand Down

0 comments on commit cb5dc7f

Please sign in to comment.