# Searching for Messages

The `SearchInputView` and `SearchResultListView` components can be used to search and display messages that contain specific text. The search is performed across all channels a user is a member of.

| Light Mode                                                                          | Dark Mode                                                                         |
| ----------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
| ![search view light](@chat-sdk/android/v6-latest/_assets/search_view_hey_light.png) | ![search view dark](@chat-sdk/android/v6-latest/_assets/search_view_hey_dark.png) |

## Usage

Here's an example layout using these two Views:

```xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <io.getstream.chat.android.ui.feature.search.SearchInputView
        android:id="@+id/searchInputView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <io.getstream.chat.android.ui.feature.search.list.SearchResultListView
        android:id="@+id/searchResultListView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/searchInputView" />

</androidx.constraintlayout.widget.ConstraintLayout>
```

We recommend using `SearchViewModel` to get search results from the Stream API and then render them using the `SearchResultListView`.

The basic setup of the ViewModel and connecting it to the View is done the following way:

<tabs>

<tabs-item value="kotlin" label="Kotlin">

```kotlin
// Instantiate the ViewModel
val searchViewModel: SearchViewModel by viewModels()

// Bind the ViewModel with SearchResultListView
searchViewModel.bindView(searchResultListView, viewLifecycleOwner)
```

</tabs-item>

<tabs-item value="java" label="Java">

```java
// Instantiate the ViewModel
SearchViewModel viewModel = new ViewModelProvider(this).get(SearchViewModel.class);

// Bind it with SearchResultListView
SearchViewModelBinding.bind(viewModel, searchResultListView, getViewLifecycleOwner());
```

</tabs-item>

</tabs>

Finally, start the search by passing the search query to the ViewModel:

<tabs>

<tabs-item value="kotlin" label="Kotlin">

```kotlin
// Notify ViewModel when search is triggered
searchInputView.setSearchStartedListener(viewModel::setQuery)
```

</tabs-item>

<tabs-item value="java" label="Java">

```java
// Notify ViewModel when search is triggered
searchInputView.setSearchStartedListener(viewModel::setQuery);
```

</tabs-item>

</tabs>

<admonition type="note">

`bindView` sets listeners on the view and the ViewModel. Any additional listeners should be set _after_ calling `bindView`.

</admonition>

## Handling Actions

In addition to the `SearchStartedListener` described above, `SearchInputView` allows you to listen for text changes by using listeners:

<tabs>

<tabs-item value="kotlin" label="Kotlin">

```kotlin
searchInputView.setContinuousInputChangedListener { query ->
    // Search query changed
}
searchInputView.setDebouncedInputChangedListener { query ->
    // Search query changed and has been stable for a short while
}
```

</tabs-item>

<tabs-item value="java" label="Java">

```java
searchInputView.setContinuousInputChangedListener(query -> {
    // Search query changed
});
searchInputView.setDebouncedInputChangedListener(query -> {
    // Search query changed and has been stable for a short while
});
```

</tabs-item>

</tabs>

`SearchResultListView` exposes a listener for handling item clicks:

<tabs>

<tabs-item value="kotlin" label="Kotlin">

```kotlin
searchResultListView.setSearchResultSelectedListener { message ->
    // Handle search result click
}
```

</tabs-item>

<tabs-item value="java" label="Java">

```java
searchResultListView.setSearchResultSelectedListener(message -> {
    // Handle search result click
});
```

</tabs-item>

</tabs>

The full list of listeners available for `SearchInputView` can be found [here](https://getstream.github.io/stream-chat-android/stream-chat-android-ui-components/io.getstream.chat.android.ui.feature.search/-search-input-view/), and for `SearchResultListView` [here](https://getstream.github.io/stream-chat-android/stream-chat-android-ui-components/io.getstream.chat.android.ui.feature.search.list/-search-result-list-view/).

## Updating the Search Query Programmatically

`SearchInputView` provides a way to change the search query programmatically:

<tabs>

<tabs-item value="kotlin" label="Kotlin">

```kotlin
searchInputView.setQuery("query")
```

</tabs-item>

<tabs-item value="java" label="Java">

```java
searchInputView.setQuery("query");
```

</tabs-item>

</tabs>

You can also easily clear the current input:

<tabs>

<tabs-item value="kotlin" label="Kotlin">

```kotlin
searchInputView.clear()
```

</tabs-item>

<tabs-item value="java" label="Java">

```java
searchInputView.clear();
```

</tabs-item>

</tabs>

<admonition type="note">

Updating the search query programmatically automatically notifies corresponding listeners.

</admonition>

---

This page was last updated at 2025-08-15T14:15:37.970Z.

For the most recent version of this documentation, visit [https://getstream.io/chat/docs/sdk/android/ui/utility-components/search-view/](https://getstream.io/chat/docs/sdk/android/ui/utility-components/search-view/).