- A list to display search results
- A search box to type your query
- Statistics about the current search
- A facet list for filtering results
Installation
To use Algolia with InstantSearch Android, you need an Algolia account. You can sign up for a new account, or use the following credentials:- Application ID:
latency
- Search API Key:
1f6fd3a6fb973cb08419fe7d288fa4db
- Index 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
build.gradle
, add the following dependency:
build.gradle
build.gradle
AndroidManifest.xml
must include the following permissions:
XML
kotlinx.serialization
by adding the serialization plugin to your build.gradle
:
build.gradle
Implementation
Architecture overview
MainActivity
: This activity controls the fragment currently displayed.MyViewModel
: AViewModel
from Android Architecture Components. The business logic lives here.ProductFragment
: This fragment displays a list of search results in aRecyclerView
, aSearchView
input, and aStats
indicator.FacetFragment
: This fragment displays a list of facets to filter your search results.
Initializing a Searcher
The central part of your search experience is the Searcher
. The Searcher
performs search requests and obtains search results. Most InstantSearch components are connected with the Searcher
.
In this tutorial you will only target one index, so instantiate a HitsSearcher
with the proper credentials.
Go to MyViewModel.kt
file and add the following code:
Kotlin
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.

LiveData
object, which holds a PagedList
of Product
.
Create the Product
data class which contains a single name
field.
Kotlin
product_item.xml
file.
XML
ProductViewHolder
to bind a Product
item to a RecyclerView.ViewHolder
.
Kotlin
ProductAdapter
by extending PagingDataAdapter
. The ProductAdapter
binds products to the ViewHolder
.
Kotlin
Paginator
with your searcher. Do this in your ViewModel
:
Kotlin
ViewModel
has some data, you can create a simple product_fragment.xml
with a Toolbar
and a RecyclerView
to display the products:
XML
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
ProductFragment
, update activity_main.xml
to have a container for the fragments:
XML
MainActivity
to display ProductFragment
:
Kotlin
RecyclerView
.
Searching your data: SearchBox
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.SearchView
in your Toolbar
:
XML
SearchBoxViewAppCompat
to the SearchBoxConnectorPagedList
stored in MyViewModel
using a new ConnectionHandler
that conforms to the ProductFragment
lifecycle:
Kotlin
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 achieve this in a few lines.
Add a StatsConnector
to your MyViewModel
, and connect it with a ConnectionHandler
.
Kotlin
TextView
to your product_fragment.xml
file to display the stats.
XML
StatsConnector
to a StatsTextView
in your ProductFragment
.
Kotlin
Filter your data: FacetList

FacetList
to filter products by category.
Create a drawable resource ic_check.xml
. This resource displays checked filters.
XML
facet_item.xml
file. This is the layout for a RecyclerView.ViewHolder
.
XML
FacetListViewHolder
and its Factory
, so that later on it works with a FacetListAdapter
.
Kotlin
FilterState
.
Pass the FilterState
to your FacetListConnector
. The FacetListConnector
needs an Attribute
: you should use category
.
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 theFilterState
, and applies its filters with every search. - The
FilterState
connects to your productsPaginator
to invalidate search results when new filter are applied. - Finally, the
facetList
connects to its adapter.
Kotlin
facet_fragment.xml
layout with a RecyclerView
.
XML
FacetFragment
and configure your RecyclerView
with its adapter and LayoutManager
.
Kotlin
FacetFragment
in MainActivity
with “navigation up” support.
Kotlin
product_fragment.xml
XML
displayFilters
and navigateToFilters()
to MyViewModel
to trigger filters display:
Kotlin
ProductFragment
, add a listener to the filters button to switch to FacetFragment
:
Kotlin
FacetFragment
:
Kotlin
Improving the user experience: Highlighting
Highlighting enhances the user experience by putting emphasis on the parts of the result that match the query. 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
.
First, define a highlightedName
field to retrieve the highlighted value for the name
attribute.
Kotlin
.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 how many records are returned and why they’re relevant to the query.