- How to add Search in RecyclerView using Kotlin
- Adding SearchView in RecyclerView
- Customizing the SearchView
- Kotlin RecyclerView SearchView Example Tutorial | Search Filter
- Step 1. New Studio Project
- Step 2. Gradle Changes
- Step 3. XML In Drawable
- Step 4. Special XML for Recycler Items And Model
- Step 5. Adapter For Search
- Step 6. Last but Main Activity
- Kotlin Listview Searchview Android Tutorial Example
- Java Code
- Step 1. New Project For Kotlin
- Step 2. Separate Row file for Listview
- Step 3. Making Model File
- Step 4. Writing the Adapter Class
- Step 5. Main Activity Writings
- Understand Main Ativity
How to add Search in RecyclerView using Kotlin
UPDATE [April 17th, 2021]: Switched from Kotlinx synthetic to View Binding. If you haven’t implement View Binding in your project yet, check this tutorial.
Today, I will show you how to search for items in a RecyclerView using SearchView.
In this example, we have a RecyclerView already set up, and the only thing we do is to add the searching functionality.
We have a list of countries as sample data and using the search field we get the country we want faster.
Adding SearchView in RecyclerView
Add the SearchView in your layout, inside a LinearLayout with a vertical orientation.
In the RecyclerView adapter (In this example, RecyclerView_Adapter.kt), create an ArrayList with a name countryFilterList, and pass all the items..
Return the size of countryFilterList so every time will return the right amount of items that match the characters you typing in the SearchView.
Now, in the onBindViewHolder get the item for each row from the countryFilterList array.
Add the Filterable class in the RecyclerView_Adapter and the method getFilter()
Inside the getFilter() method, return a Filter() object:
The performFiltering method checks if we have typed a text in the SeachView.
If there is not any text, will return all items.
If there is a text, then we check if the characters match the items from the list and return the results in a FilterResults type.
The publishResults get these results, passes it to the countryFilterList array and updates the RecyclerView.
Go back to the Activity file (MainActivity.kt) and add the setOnQueryTextListener.
The onQueryTextChange method called every time we typing on the SearchView and updates the RecyclerView with the new results.
Customizing the SearchView
If you want to change the color of the search icon (magnifying glass):
For the color of the cancel button:
And to change the text color of the TextView:
If you have any questions, please feel free to leave a comment below
Источник
Kotlin RecyclerView SearchView Example Tutorial | Search Filter
I am writing on Kotlin RecyclerView SearchView Example Tutorial.
You will learn to implement Search Filter functionality in the RecyclerView in Android using Kotlin.
When there are many items in the recycler view, search filter functionality helps user to find his desired item quickly.
First, see the following for output reference.
Step 1. New Studio Project
Make a new project in android studio with empty activity as a default one.
Select Kotlin as the primary source language for the entire project.
Step 2. Gradle Changes
Open your build.gradle(Module:app) file and add the following lines in it
First line is integrating the classes for recycler view. We will be able to use Recycler view with the help of this line.
Second line is doing the same thing for card view. Card view helps us to make better design with recycler view.
Step 3. XML In Drawable
Navigate to the app->res->drawable folder and create a new XML file in this drawable folder.
Set the name of the XML file as cardview.xml . You need to add the following code lines in this cardview.xml file.
This file creating some gradient effects with various colors. Using this file, we can design our card view more efficiently.
Using this file, we can also add corners to our card view. At any time, you can change the colors or a radius size.
Step 4. Special XML for Recycler Items And Model
Make a new XML resources file in app->res->layout directory and give it a name like rv_item.xml
You should write down the following source line in rv_item.xml
This file will help us to create the look and feel of the every row item of the recycler view.
A file cardview.xml that we have created in the drawable folder is used here in the first text view.
Now make a new Kotlin file and give it a name like SearchModel.kt
Below source lines for SearchModel.kt file.
A string variable name is defined in this model file.
For this variable, I have written getter and setter methods. These methods will help to maintain the data during search activity is going on.
Step 5. Adapter For Search
Make a new class and give it a name as SearchAdapter.kt
You need to write down the following code snippet in this file.
See the following first
Above code is of onCreateViewHolder() method. It is inflating the rv_item.xml file. Using this line compiler will create the view for every child item of the recycler view.
Read the following now
A method onBindViewHolder() will set the text into the text view. For this, it will imageModelArayList which is the data source. Adapter is getting this arraylist from the parameter.
We will create this arraylist in the main activity to see which kind of data it contains.
Now check the following method
This function is the main and important portion of the whole example.
As it’s name suggests, filter() method will filter the recycler view on the basis of it’s name.
Whenever the user types the search query, compiler will call the above function. It will send the search query in to the parameter of this method.
Based on this search query, compiler will update the items in the imageModelArraList and then it will call the notifyDataSetChanged() method.
notifyDataSetChanged() method will simply simply update the recycler view as per the new imageModelArraList.
Step 6. Last but Main Activity
Now this is the last step of our example. Go to the activity_main.xml file and add the below code in it
It has only two UI widgets. One is search view and another is recycler view.
Search view is above the Recycler view so that user can type the search query and it improves over all user experience.
Now in the MainActivity.kt file, you should write the following data lines
See the following
Compiler will first create the objects of recycler view, Search adapter and Search View classes.
Then an string array myImageNameList is there. It contains the names of the various vehicles. We will filter the recycler view based on this vehicle names.
This line will use the populateList() method to create the data source. Below is the code for populateList() method
It will create one arraylist which contains the objects of the SearchModel class. Then one for loop is there.
This for loop will create an object of the SearchModel class in it’s every iteration and it will bind one vehicle name to each object. Then all objects are added into the arraylist.
Read the below code
Compiler will run the above when the user clicks the recycler view. It will simply create the Toast. This Toast contains the name of the clicked Vehicle.
Now focus on the below
Compiler will first find the Search View using id. Then it will set the onQuery change.
It will run the following when the user types the query
Compiler will execute the method filter() which is written in the SearchAdapter class.
Источник
Kotlin Listview Searchview Android Tutorial Example
Welcome to kotlin listview searchview tutorial example.
In this article, you will learn how to implement search filter functionality for listview using the searchview.
Search filter is very basic functionality and you will need this in almost every case where members of listview is very high.
Java Code
If you are looking for the same tutorial in JAVA language then read : Android SearchView ListView in JAVA
First of all, see the below output result of this example.
Step 1. New Project For Kotlin
In this step ,you need to make a new project in android studio. Here, you need to add kotlin support or select project language as a kotlin.
Along with adding kotlin support, you also need to select empty activity as a default activity.
Step 2. Separate Row file for Listview
Let us make a new layout file that will create a specific view for each listview item.
Inside, res->layout directory, add a new XML layout file and give it a name like lv_item.xml
You should add the following source snippet in lv_item.xml file.
Step 3. Making Model File
Now create a new Kotlin file or class and give it a name like MovieNames.kt
Below is the source code lines for MovieNames.kt file.
This MovieNames.kt file is working as a model class for listview.
We will create an object of this class for each listview row item. We will do this in the Main Activity.kt class.
See the source code of this class. It contains one constructor. There is one string variable in this constructor.
Above the constructor, we have defined one simple string variable. We will assign the value of the constructor string variable to the simple string variable.
Then there is a method called getAnimalName()
This getAnimalName() method will return the value of the simple string variable.
Step 4. Writing the Adapter Class
Make a new kotlin class and set it’s name as ListViewAdapter.kt
In this ListViewAdapter.kt class, write down the following source code lines
An adapter class will bind the data source with the listview. Here, our data source will be the arraylist with the objects of MovieNames.kt class. We are getting this data source from below line
Main Activity contains the movieNamesArrayList as a public access specifier. So we can use it in this adapter class.
See the following source lines
This method will create the view for each child row of listview.
It will inflate the lv_item.xml file as the base of creating look and feel of row item.
lv_item.xml file contains one text view. Compiler will use movieNamesArrayList o set the name of the movie in this text view.
Now see the following lines
A filter() function is the heart of this search view listview kotlin tutorial.
This function will get the search query via it’s parameter. Then it will save this query in one separate variable.
Then clear the movieNamesArrayList . Now it will check how many words are there in search query. If zero then it will again fill all the movie names in movieNamesArrayList
But if not zero then it will create one for loop. In every iteration of for loop, compiler will get the movie name and will check if it contains the words of search query. If yes then it will simply add this movie name in movieNamesArrayList otherwise not.
Step 5. Main Activity Writings
You should have two files for main activity : activity_main.xml and MainActivity.kt
In your activity_main.xml file, add the following code snippet
One search view and one listview is there in this XML file. Search is above the listview.
Now in MainActivity.kt file, write down the below source lines
Understand Main Ativity
Let us read each line from above snippet.
First of all, see the below code
Above snippet is declaring four variables. First one is listview object, second one is adapter object, third is searchview object and last one is String arraylist.
Now see the following lines
We are just filling the string array variable with the names of the movies. This will be the primary data source.
Now look at the following code lines
movieNamesArrayList is the arraylist with the objects of the MovieNames classes.
First line is initializing the variable. Then there is one for loop.
Every iteration of for loop will make a new object of MovieNames class. In the parameter, we will add the movie name from string array variable moviewList
After creating an object, we will add this object into movieNamesArrayList . Thus one iteration of for loop is complete.
Number of for loop iterations are equal to the number of members in the string array variable moviewList
Now focus on the below code
First line is initializing the adapter object.
Second one is simply binding the adapter to the listview.
Now read the below code
First one is finding the searchview from id and second one is defining a query listener.
When you define the search query listener, you need to override two functions and they are as the following
When the user types the search query, compiler will run the second function onQueryTextChanged() and it will execute the filter() function which is written in the ListViewAdapter.kt class.
filter() function will send the search query as it’s parameter.
Now see the below
Compiler will run the above code when the user clicks the listview. It will pop up one toast and will write the name of the movie in the toast.
Источник