> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SearchBox

> Lets users perform a search using text input.

export const SearchQuery = () => <Tooltip tip="The text users enter into a search box. In the Search API, this corresponds to the query parameter. A search query is often used with filters, facets, and other parameters, but these aren't part of the query text itself.">
    search query
  </Tooltip>;

<div className="not-prose algolia-flavor-switcher">
  <div className="afs-dropdown">
    <div className="afs-trigger" role="button" tabIndex="0" aria-haspopup="listbox">
      <span className="afs-current">Android</span>

      <svg className="afs-chevron lucide lucide-chevron-down" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
        <path d="m6 9 6 6 6-6" />
      </svg>
    </div>

    <ul className="afs-menu" role="listbox">
      <li role="option" aria-selected="false"><a className="afs-option" href="/doc/api-reference/widgets/search-box/js"><span className="afs-option-name">JavaScript</span><span className="afs-option-lib">InstantSearch.js</span></a></li>
      <li role="option" aria-selected="false"><a className="afs-option" href="/doc/api-reference/widgets/search-box/react"><span className="afs-option-name">React</span><span className="afs-option-lib">React InstantSearch</span></a></li>
      <li role="option" aria-selected="false"><a className="afs-option" href="/doc/api-reference/widgets/search-box/vue"><span className="afs-option-name">Vue</span><span className="afs-option-lib">Vue InstantSearch</span></a></li>
      <li role="option" aria-selected="false"><a className="afs-option" href="/doc/api-reference/widgets/search-box/ios"><span className="afs-option-name">iOS</span><span className="afs-option-lib">InstantSearch iOS</span></a></li>
      <li role="option" aria-selected="true"><a className="afs-option is-current" href="/doc/api-reference/widgets/search-box/android"><span className="afs-option-name">Android</span><span className="afs-option-lib">InstantSearch Android</span></a></li>
    </ul>
  </div>
</div>

```kotlin Signature theme={"system"}
  SearchBoxConnector(
      searcher: Searcher
      viewModel: SearchBoxViewModel
      searchMode: SearchMode
      debouncer: Debouncer
      searchOnQueryUpdate: Boolean
  )
```

<Card title="Explore example code" icon="github" href="https://github.com/algolia/instantsearch-android/blob/master/examples/android/src/main/kotlin/com/algolia/instantsearch/examples/android/showcase/androidview/search/SearchAsYouTypeShowcase.kt">
  Browse the SearchBox example code on GitHub.
</Card>

## About this widget

The `SearchBox` is used to perform a text-based <SearchQuery />.

To add a `SearchBox` to your search experience, use these components:

* `Searcher`. The [`Searcher`](/doc/api-reference/widgets/instantsearch/android) that handles your searches.
* `SearchBoxViewModel`. The business logic that handles new search inputs.
* `SearchBoxView`. The view that handles the input.

See also: [Getting started with imperative UI](/doc/guides/building-search-ui/getting-started/how-to/programmatically/android)

## Examples

```kotlin Kotlin icon=code theme={"system"}
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = "YourApplicationID",
        apiKey = "YourSearchOnlyAPIKey",
        indexName = "YourIndexName"
    )
    val searchBox = SearchBoxConnector(searcher)
    val connection = ConnectionHandler(searchBox)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val searchView = SearchView(this)
        val view: SearchBoxView = SearchBoxViewAppCompat(searchView)
        connection += searchBox.connectView(view)
        searcher.searchAsync()
    }

    override fun onDestroy() {
        super.onDestroy()
        connection.disconnect()
        searcher.cancel()
    }
}
```

## Parameters

<ParamField body="searcher" type="Searcher" required>
  The [`Searcher`](/doc/api-reference/widgets/instantsearch/android) that handles your searches.
</ParamField>

<ParamField body="viewModel" type="SearchBoxViewModel" post={["default: SearchBoxViewModel()"]}>
  The business logic that handles new search inputs.
</ParamField>

<ParamField body="searchMode" type="SearchMode" post={["default: SearchMode.AsYouType"]}>
  Defines the event triggering a new search.

  * `SearchMode.AsYouType`. Triggers a search on each keystroke.
  * `SearchMode.OnSubmit`. Triggers a search on submitting the query.
</ParamField>

<ParamField body="debouncer" type="Debouncer" post={["default: Debouncer(debounceLoadingInMillis)"]}>
  Delays searcher operations by a specified time duration.
</ParamField>

<ParamField body="searchOnQueryUpdate" type="Boolean" post={["default: true"]}>
  Controls whether the [`Searcher`](/doc/api-reference/widgets/instantsearch/android) automatically runs a search when the query changes. When `true` (default), typing or submitting updates the query and triggers a search. When `false`, the query updates but no search is sent until you trigger it manually, giving you full control over when searches occur.
</ParamField>

## View

<ParamField body="searchBoxView" type="SearchBoxView" required>
  The view that handles the input.

  ```kotlin Kotlin icon=code theme={"system"}
  val searchView = SearchView(this)
  val view: SearchBoxView = SearchBoxViewAppCompat(searchView)
  searchBox.connectView(view)
  ```
</ParamField>

### Compose UI

InstantSearch provides `SearchBoxState` as a state model, which is an implementation of the `SearchBoxView` interface.
You need to connect `SearchBoxState` to the `SearchBoxConnector` or `SearchBoxViewModel` like any other `SearchBoxView` implementation,
and create a Compose UI view that handles the query input, you can directly use the `SearchBoxState` as a state model,
It provides the `query` property along with the `setText` function to streamline the design process of your custom Compose UI view.

```kotlin Kotlin icon=code theme={"system"}
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = "YourApplicationID",
        apiKey = "YourSearchOnlyAPIKey",
        indexName = "YourIndexName"
    )
    val searchBoxState = SearchBoxState()
    val searchBox = SearchBoxConnector(searcher)
    val connections = ConnectionHandler(searchBox)

    init {
        connections += searchBox.connectView(searchBoxState)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TextField(
                // set query as text value
                value = searchBoxState.query,
                // update text on value change
                onValueChange = { searchBoxState.setText(it) },
                // set ime action to "search"
                keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
                // set text as query submit on search action
                keyboardActions = KeyboardActions(
                    onSearch = { searchBoxState.setText(searchBoxState.query, true)}
                )
            )
        }
        searcher.searchAsync()
    }

    override fun onDestroy() {
        super.onDestroy()
        connections.disconnect()
        searcher.cancel()
    }
}
```
