AI Features

Search

This lesson will teach you how to add a search functionality to the toolbar and filter items in the blog list screen.

We'll cover the following...

Final result preview

Toolbar menu

In a similar manner to how we added sort item to the toolbar menu, we can add search item with few adjustments:

  • To always show the search icon, we will use the showAsAction="always" attribute and specify icon via icon attribute.
  • The logic to expand the search icon to the search field is already available in the Android framework. We can use it by setting actionViewClass attribute value to the
...
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search_white_24px"
android:title="Search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
<item
android:id="@+id/sort"
android:title="Sort"
app:showAsAction="never" />
</menu>
...