![]() |
VOOZH | about |
Many apps represent data in the form of huge lists and for filtering these lists we have seen the SearchView present inside these apps. So for filtering this list of data we generally use a SearchView. In this article, we will take a look at the implementation of Search View in Android with a RecyclerView in Android.
We will be building a simple application in which we will display a list of data in the RecyclerView. We will be displaying the list of courses with its description and on that recycler view, we will add a SearchView which will be used to filter the data in our Recycler View. A sample video is given below to get an idea about what we are going to do in this article.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
The code for that has been given in both Java and Kotlin Programming Language for Android.
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
activity_main.xml:
Navigate to the app > java > your app's package name > Right-click on it > New > Java/Kotlin class and name your class as CourseModel and add the below code to it.
CourseModel File:
Navigate to the app > res > layout > Right-click on it > New > layout resource file and name your layout as course_rv_item and add the below code to it.
course_rv_item.xml:
Navigate to the app > java > your app's package name > Right-click on it > New > Java/Kotlin class and name it as CourseAdapter and add the below code to it.
CourseAdapter File:
Navigate to the app > res > Right-click on it > New > Directory and give a name to your directory as the menu. After creating a new directory we have to create a new menu file inside it. For creating a new menu file. Navigate to the app > res > menu > Right-click on it > New > menu resource file, name it as search_menu and add the below code to it.
search_menu.xml:
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.
MainActivity File:
In the newer versions of Android Studio, the action bar is removed by default. To change that, navigate to app > res > values > themes.xml and update the base theme style
Before
<style name="Base.Theme.Demo" parent="Theme.Material3.DayNight.NoActionBar">After
<style name="Base.Theme.Demo" parent="Theme.Material3.DayNight">Click Here to get the full Project for SearchView with RecyclerView