Search

Learn how to add search functionality to the toolbar and filter items in the blog list screen in this lesson.

We'll cover the following...

Final result preview

Toolbar menu

Much like 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 the 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 the actionViewClass attribute value to the android.widget.SearchView.

Press + to interact
<?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:title="Search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
<item
android:id="@+id/sort"
android:title="Sort"
app:showAsAction="never" />
</menu>

With just a ...