Android SearchView.

| Page Views: 1543

Android SearchView Tutorial and Examples.

Android SearchView tutorial and examples. This piece will introduce you to searchview then provide our other tutorials links for actually working with SearchView.

What is SearchView?

A SearchView is a view that allows users to search/filter data by typing text into it.

To search data you need enter text or query or search term. You can definitely use an edittext. However searchview is better in that it's been designed specifically for searching while an edittext is more generic.

For example, searchview can be iconified by easily using setIconifiedByDefault(true) while for an edittext you have to create a new custom edittext.

SearchView API Definition

SearchView was added in android API Lelevl 11.

As a class it derives from LinearLayout and implements CollapsibleActionView.

A CollapsibleActionView is an interface that provides callbacks to implementers that allow them know when the View has been expanded or collapsed as an action view.

public class SearchView
extends LinearLayout implements CollapsibleActionView

Here's it's the inheritance hierarchy of SearchView.


java.lang.Object
   ↳    android.view.View
       ↳    android.view.ViewGroup
           ↳    android.widget.LinearLayout
               ↳    android.widget.SearchView

Commonly used SearchView interfaces

(a) SearchView.OnQueryTextListener

Callbacks for changes to the query text.

This has two abstract methods to be implemented:

No. return type method Description
1. abstract boolean onQueryTextChange(String newText) Called when the query text is changed by the user.
2. abstract boolean onQueryTextSubmit(String query) Called when the user submits the query.

Here's an example usage in Kotlin:

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText.isNullOrEmpty()) {
                    if (popupWindow.isShowing) {
                        popupWindow.dismiss()
                    }
                } else {
                    executor.execute {
                        val result = onQueryTextListener.invoke(newText!!)
                        queryList.clear()
                        if (result.isNotEmpty()) {
                            queryList.addAll(result)
                        }
                        searchView.post { refreshQueryView(queryList.isNotEmpty()) }
                    }
                }
                return false
            }
        })
(b). SearchView.OnSuggestionListener

This is a Callback interface for selection events on suggestions. These callbacks are only relevant when a SearchableInfo has been specified by SearchView.setSearchableInfo(SearchableInfo).

No. return type method Description
1. abstract boolean onSuggestionClick(int position) This is called when a suggestion was clicked.
2. abstract boolean onSuggestionSelect(int position) This is called when a suggestion was selected by navigating to it.

Here's an example usage:

        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setIconifiedByDefault(false);
        searchView.requestFocus();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
            @Override
            public boolean onSuggestionSelect(int position) {

                return false;
            }

            @Override
            public boolean onSuggestionClick(int position) {
                SQLiteCursor cursor = (SQLiteCursor) searchView.getSuggestionsAdapter().getItem(position);
                int indexColumnSuggestion = cursor.getColumnIndex( SuggestionsDatabase.FIELD_SUGGESTION);
                getsearch(cursor.getString(indexColumnSuggestion));
                cursor.close();

                return true;
            }
        });
SearchView XML Attributes
No. Attribute Description Java Alternative
1. android:iconifiedByDefault The default state of the SearchView. If true, it will be iconified when not in use and expanded when clicked.May be a boolean value, such as "true" or "false". setIconifiedByDefault(boolean)
2. android:inputType The input type to set on the query text field.Can be date,datetime,number,text, et. setInputType(int)
3. android:queryHint An optional query hint string to be displayed in the empty query field. setQueryHint(CharSequence)

Find more attributes here.

Top Android SearchView Examples

Let's look at some examples.

Android Custom ListView with Images and Text Search Filter

How to search filter a custom ListView with images and text using a searchview. Our adapter is BaseAdapter.

Android Custom ListView Search/Filter and ItemClickListener tutorial.

In this piece we see how to search/filter a ListView then handle the itemClick events for the searched cardviews.

Android Custom RecyclerView with Images and Text Search/Filter.

This is an android tutorial to explore how to search or filter a recyclerview with images and text.

Android Material SearchBar Tutorial and Example.

  • How to use a Material SearchView inside a Searchbar and show search history.
  • The user can select search history as suggestions instead of typing again.
  • In this case we stored the search history in a simple arraylist.

Android Material SearchView and History Tutorial and Example.

  • Here the purpose is to programmatically open search,close and show search suggestions while searching.
  • The search occurs in a nice fragment.
  • We use a Material SearchView library.

Android Material ToolBar SearchBar/SearchView Example - How to filter search a simple GridView.

Android Material ToolBar SearchBar/SearchView Example - How to filter search a simple ListView and handle onclicks.

===

So let's see how to filter/search a simple ListView from the toolbar using a material design searchbar. We'll place our searchbar/searchview in the appbar and filter our listv...

Android RecyclerView Search/Filter and OnClick Tutorial and Example.

This is an android tutorial to explore how to search or filter a recyclerview with images and text.

Android RecyclerView MaterialSearchBar Search Filter.

We had done a tutorial some months later about Search/Filter with recyclerView. However, that was with the searchview as our search form. Today we see how to search filter with material searchbar as the search component. User will perform realtime search filter. As they type we filter the cards based on the search term. Our recyclerview comprises of material cardviews. We populate our recyclerview via an arraylist of strings. Let's go.

Android Simple ListView Search/Filter Tutorial and Example.

How to search or filter a simple ListView in android.

This is a Kotlin android SearchView and ListView example. We see how to search/filter a simple ListView.

MaterialSearchBar is a beautiful and easy to use library will help to add Lollipop Material Design SearchView in your project.

Best Regards, Oclemy.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.




Recommendations


What do You Think


Previous Post Next Post