How to implement searchView widget in android Studio through Recycleview?

Please Share On:

Today, I am going to show you how can you implement your searchView widget through recyclerView in android studio.

1) Create your seachView.xml

Here, add your searchView XML code where you want to add your searchView field.

<androidx.appcompat.widget.SearchView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/search_bar"
                android:layout_margin="5dp"
                android:background="@color/white"
                app:queryHint="@string/hint_search"
                app:iconifiedByDefault="false"/>

2) Coding Parts

Now, let’s move to the code section. Follow these steps to make your seachView work.

Steps to follow:

2.1) Make your adapter implements Filterable. See an example below:

public class CollectExpandableListAdapter extends BaseExpandableListAdapter implements Filterable {
//......Your all code goes here..
}

2.2) Add the below line

private List<Parent> originalList;

Now, your adapter.java looks like below:

public class CollectExpandableListAdapter extends BaseExpandableListAdapter implements Filterable {
    private static final String TAG = "tag";
    private final Context _context;
    private List<Parent> _listDataHeader; // header title
    private List<Parent> originalList;
    private final HashMap<Parent, List<Child>> _listDataChild;

//...Rest code...
}

2.3) Add the below code to your Activity class.

 SearchView searchView;

// initialize the variable
       searchView = view.findViewById(R.id.search_bar);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Log.d("newText1",query);
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                if ( TextUtils.isEmpty ( newText ) ) {
                    expListAdapter.getFilter().filter("");
                } else {
                    expListAdapter.getFilter().filter(newText.toString());
                }
                return true;
            }
        });

2.4) Finally, write some code in your adapter class. Copy the below code:

 public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                final FilterResults filterResults= new FilterResults();
                final List<Parent> results = new ArrayList<Parent>();
                if (originalList == null)
                    originalList = _listDataHeader;
                if (constraint != null) {
                    assert originalList != null;
                    if (originalList.size() > 0) {
                        for (final Parent item : originalList) {
                            if (item.getPostcode().toLowerCase().contains(constraint.toString()) ||
                                    (item.getAddress().toLowerCase().contains(constraint.toString())) ||
                                        (item.getState().toLowerCase().contains(constraint.toString())))
                            {
                                results.add(item);
                            }
                        }
                    }
                    filterResults.values = results;
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                _listDataHeader = (ArrayList<Parent>) results.values;
                notifyDataSetChanged();

            }
        };

That’s it. You have successfully created your searchView working. Now check your searchView and see the results.



You may be interested in the following topics:

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright @2023. All Right Reserved.


Social media & sharing icons powered by UltimatelySocial