SearchView Introduction

SearchView is a User Interface widget that provides search features for any query. It shows the suggestions as a query result and allows the user to pick from the suggestions. In Action Bar, SearchView can collapse as a menu item. The preferred way to provide search is in Action Bar of the Android application. In this tutorial, you will learn how to create and use SearchView in Android application. This tutorial will also explain the concept of SearchView in Action Bar use of Android Search bar example for ListView.

Android Search Bar Example with ListView

First of all, implement a ListView and populate it with the help of an Adapter for Android Search Bar Example.

Create ListView

Open the layout file of the activity, where you want to implement the ListView. Create a ListView either by drag and drop or through the code.

ListView in Android

1

2

3

4

5

6

<ListView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/listView1"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true"/>

Populate ListView

You can populate the ListView with the help of Adapter as well as you can use RecyclerView. If you want to load data from some online source that provides JSON, then use Retrofit library with Gson to get JSON data in your application. This Android Search Bar example uses simple Adapter with fixed array to populate the ListView for better understanding.

Create a string array and use ArrayAdapter to populate the ListView. Also, add the click listener for ListView item click.

Until now, Android Search Bar Example just have a ListView which is populated with the help of ArrayAdapter.

Components of Search Bar

Moving further, create a new Search widget. Before implementing the Android Search Bar example widget. You must know about the components of the Search bar.

A search bar is comprised of a menu item which acts as Action bar search widget to display the search field. i.e. to show the Search Bar. And a searchable layout file. It defines the behavior of the SearchView. This searchable layout file contains the hint (What Search is for).

Search menu resource

The very first step to Search bar is to create a menu resource file. So first create a new menu folder in the resource(res) directory and add a new menu file. This menu file contains the search widget and its properties i.e. the title of the SearchView, Id, and other properties.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<menu xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:yourapp="http://schemas.android.com/tools">

<!--Search Widget-->

<item

app:showAsAction="always|collapseActionView"

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/listsearch"

app:actionViewClass="android.support.v7.widget.SearchView"

android:title="Search"

android:iconifiedByDefault="true"

android:showAsAction="ifRoom|withText|always|collapseActionView"

android:actionViewClass="android.widget.SearchView"/>

</menu>

Searchable Configuration

To describe the behavior of the Search Bar, create a searchable configuration. So create a new xml folder in the resource(res) directory and add a new layout file.

nice tutorial, I have an error in mainactivity for cannot resolve method getFilter() and cannot resolve method getItemAtPosition

any solution?

Beginners Heap Free Tutorials

Thankyou! ArrayAdapter has built-in getFilter() method. If you are not using ArrayAdapter you must implement Filterable. Compare your code with the attached source files to track the error.

Best Regards, Beginners Heap

Muhammad Salma Nabila Alibasyi

that’s not work for me I have tried to use recyclerview with retrofit and implement your code, but there is still error with cannot resolve method getFilter() and cannot resolve method getItemAtPosition.

Beginners Heap Free Tutorials

You can use recyclerView.getAdapter().getList().get(position) instead of getItemAtPosition(). There is also a RecyclerView example -> https://www.beginnersheap.com/card-view-recycler-view-example-tutorial/ in which a Toast is displayed on item click. In the case of RecyclerView you have a separate Adapter extends RecyclerView.Adapter. you must implement Filterable so your adapter will look like Adapter extends RecyclerView.Adapter implements Filterable. in your adapter class implement getFilter() method logic to use getFilter method. There are many other examples to implement getFilter() logic I can’t mention all of them, Search for “Add a search filter on RecyclerView with Cards layout”

Best regards, Beginners Heap

Muhammad Salma Nabila Alibasyi

Nice, method getFilter works fine. Thankyou but when i use recyclerView.getAdapter().getList().get(position) instead of getItemAtPosition(), it’s still error (red warning). Here is my code: