API Reference / Android Widgets / SearchBox
Apr. 24, 2019

About this widget

The SearchBox is used to perform a text-based query.

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

  • Searcher: The Searcher that handles your searches.
  • SearchBoxViewModel: The business logic that handles new search inputs.
  • SearchBoxView: The view that handles the input.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class MyActivity : AppCompatActivity() {

    val client = ClientSearch(
        ApplicationID("YourApplicationID"),
        APIKey("YourAPIKey")
    )
    val index = client.initIndex(IndexName("YourIndexName"))
    val searcher = SearcherSingleIndex(index)
    val viewModel = SearchBoxViewModel()
    val connection = ConnectionHandler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val someView = SearchView(this)
        val view: SearchBoxView = SearchBoxViewAppCompat(someView)

        connection += viewModel.connectSearcher(searcher)
        connection += viewModel.connectView(view)

        searcher.searchAsync()
    }

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

Parameters

searcher
type: Searcher
Required

The Searcher that handles your searches.

1
viewModel.connectSearcher(searcher)
searchBoxView
type: SearchBoxView
Required

The view that handles the input.

1
viewModel.connectView(searchBoxView)
searchMode
type: SearchMode
default: SearchMode.AsYouType
Optional
  • SearchMode.AsYouType will trigger a search on each keystroke.
  • SearchMode.OnSubmit will trigger a search on submitting the query.
1
searchBoxViewModel.connectSearcher(searcher, searchMode = SearchMode.OnSubmit)

Did you find this page helpful?