Skip to main content
This guide walks you through the steps needed to create a full InstantSearch Android experience from scratch. This guide walks you through the steps needed to create a full InstantSearch Android experience from scratch. It includes:
  • A search box for users to type queries
  • A list to display search results
  • A list to results
  • Statistics about the search

Before you begin

To use InstantSearch, you need an Algolia account. Sign up for a new account or use the following credentials (which include a pre-loaded dataset of products appropriate for this guide):
  • : latency
  • Search : 1f6fd3a6fb973cb08419fe7d288fa4db
  • name: instant_search

Create a new project and add InstantSearch Android

In Android Studio, create a new Project:
  • On the Target screen, select Phone and Tablet
  • On the Add an Activity screen, select Empty Activity
In your app’s build.gradle, add the following dependency:
build.gradle
This guide uses InstantSearch Android with Android Architecture Components, so you also need to add the following dependencies:
build.gradle
To perform network operations in your app, AndroidManifest.xml must include the following permissions:
XML
Setup kotlinx.serialization by adding the serialization plugin to your build.gradle:
build.gradle

Implementation

Architecture overview

  • MainActivity: This activity controls the displayed fragment.
  • MyViewModel: A ViewModel from Android Architecture Components. The business logic lives here.
  • ProductFragment: This fragment displays a list of search results in a RecyclerView, a SearchView input, and a Stats indicator.
  • FacetFragment: This fragment displays a list of facets to filter your search results.

Initialize a searcher

The central part of your search experience is the Searcher. The Searcher performs a and obtains search results. Most InstantSearch components connected to the Searcher. In this guide, you’ll only target one index, so instantiate a HitsSearcher with the proper credentials. Go to MyViewModel.kt file and add the following code:
Kotlin
A ViewModel is a good place to put your data sources. This way, the data persists during orientation changes and you can share it across multiple fragments.

Display results: Hits

Suppose you want to display search results in a RecyclerView. To simultaneously provide a good user experience and display thousands of products, you can implement an infinite scrolling mechanism using the Paging Library from Android Architecture Component. Screenshot of a mobile app showing search results under 'Hits,' listing items like 'Webroot SecureAnywhere' and 'Apple® - 3.3' Lightning-to-USB 2.0 Cable' with a 'FILTERS' button. To display your results:
1

Create the data source

Create a LiveData object, which holds a PagedList of Product. Create the Product data class which contains a single name field.
Kotlin
2

Define the item layout

Create the product_item.xml file.
XML
3

Implement a ViewHolder

Create the ProductViewHolder to bind a Product item to a RecyclerView.ViewHolder.
Kotlin
4

Create an adapter

Create a ProductAdapter by extending PagingDataAdapter. The ProductAdapter binds products to the ViewHolder.
Kotlin
5

Connect the searcher and paginator

In your ViewModel use a Paginator with your searcher:
Kotlin
6

Create the fragment layoutr

Now that your ViewModel has some data, you can create a simple product_fragment.xml with a Toolbar and a RecyclerView to display the products:
XML
7

Observe and display results

In the ProductFragment, get a reference of MyViewModel using activityViewModels(). Then, observe the LiveData to update the ProductAdapter on every new page of products. Finally, configure the RecyclerView by setting its adapter and LayoutManager.
Kotlin
To display the ProductFragment, update activity_main.xml to have a container for the fragments:
XML
Update MainActivity to display ProductFragment:
Kotlin
You have now learned how to display search results in an infinite scrolling RecyclerView.
To search your data, users need an input field. Any change in this field should trigger a new request, and then update the search results. To achieve this, use a SearchBoxConnector. This takes a Searcher and connected to Pagiantor using connectPaginator.
Kotlin
Most InstantSearch components should be connected and disconnected in accordance to the Android Lifecycle to avoid memory leaks. A ConnectionHandler handles a set of Connection s for you: Each += call with a component implementing the Connection interface will connect it and make it active. Whenever you want to free resources or deactivate a component, call the disconnect method.
You can now add a SearchView in your Toolbar:
XML
Connect the SearchBoxViewAppCompat to the SearchBoxConnectorPagedList stored in MyViewModel using a new ConnectionHandler that conforms to the ProductFragment lifecycle:
Kotlin
You can now build and run your app, and see a basic search experience. The results change with each key stroke.

Displaying metadata: Stats

It’s a good practice to show the number of hits that were returned for a search. You can use the Stats components to do this with minimal code. Add a StatsConnector to your MyViewModel, and connect it with a ConnectionHandler.
Kotlin
Add a TextView to your product_fragment.xml file to display the stats.
XML
Next, connect the StatsConnector to a StatsTextView in your ProductFragment.
Kotlin
You can now build and run your app and see your new search experience in action.

Filter your data: FacetList

Screenshot of a mobile interface showing a list of categories with counts, where 'Cell Phone Cases and Clips' is selected with a checkmark. Filtering search results helps your users find what they want. You can create a FacetList to filter products by category. Create a drawable resource ic_check.xml. This resource displays checked filters.
XML
Create a facet_item.xml file. This is the layout for a RecyclerView.ViewHolder.
XML
Implement the FacetListViewHolder and its Factory, so that later on it works with a FacetListAdapter.
Kotlin
You can use a new component to handle the filtering logic: the FilterState. Pass the FilterState to your FacetListConnector. The FacetListConnector needs an attribute: you should use "categories". Inject MyFacetListViewHolder.Factory into your FacetListAdapter. The FacetListAdapter is an out of the box RecyclerView.Adapter for a FacetList. Connect the different parts together:
  • The Searcher connects itself to the FilterState, and applies its filters with every search.
  • The FilterState connects to your products Paginator to invalidate search results when new filter are applied.
  • The facetList connects to its adapter.
Kotlin
To display your facets, create a facet_fragment.xml layout with a RecyclerView.
XML
Create a FacetFragment and configure your RecyclerView with its adapter and LayoutManager.
Kotlin
Add the code to switch to FacetFragment in MainActivity with “navigation up” support.
Kotlin
Add a filters button in product_fragment.xml
XML
Add displayFilters and navigateToFilters() to MyViewModel to trigger filters display:
Kotlin
In ProductFragment, add a listener to the filters button to switch to FacetFragment:
Kotlin
Add navigation setup to display FacetFragment:
Kotlin
You can now build and run your app to see your advanced search experience. This experience helps your users to filter search results and find what they’re looking for.

Improving the user experience: Highlighting

Highlighting enhances the user experience by putting emphasis on the parts of the result that match the . It’s a visual indication of why a result is relevant to the query. You can add highlighting by implementing the Highlightable interface on Product. Define a highlightedName field to retrieve the highlighted value for the name attribute.
Kotlin
Use the .toSpannedString() extension function to convert an HighlightedString into a SpannedString that can be assigned to a TextView to display the highlighted names.
Kotlin

Conclusion

You now have a fully working search experience: your users can search for products, refine their results, and understand the number of returned and why they’re relevant to the query. Screenshot of a mobile search with 'Hello' entered, showing results for 'Hello Kitty' products and a keyboard. Find the full source code in GitHub.

Going further

This is only an introduction to what you can do with InstantSearch Android: check out the widget showcase to see more components.
Last modified on July 22, 2026