Filter RecyclerView using Search View | Android

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back again for a new video and in this video I will be telling you how to implement a search filter inside your recycler view if you don't know what a recycler view is then you can watch my video that is linkable so without wasting any time let's jump into our application and see what we have made till now so here we have our application and it is a very basic recycler view with the list of Marvel movies so now let's jump into our Android studio and see what we have there so here we have a simple main activity which implements our recycler view and then we have the recycler adapter class which implements our recycler view so now firstly what we will be going to do is that we will be adding that search icon to the very top of our app bar like somewhere here so let's go inside our application again and here in the resource folder let me create a menu directory that is a new resource directory so Android resource directory and the resource type would be menu and let's click on OK and now here inside this menu directory I will create a new XML file that will create our main menu and let's name it main menu and now let's click on OK so don't worry about this thing this is a good thing you won't be seeing that now here inside the XML file let me add in a search item so item and let's give it an ID first so ID would be action search let me close the ending tags first and now let's give it a title so title would be search now we want to provide an icon for this and since we do not have a file for our icon that is the search icon so let's go to the Straubel folder and let's create a new vector asset and here what you can do if you don't get this search I can here you can click click on this clip art thing here and here you can simply type in search and then you can find the search icon from there let's click on OK there and then we want to specify the color so I just click on this color here and then let me select on white and click choose and then next and then finish and then again it's a good thing don't worry about that now back inside my main menu dot XML let me provide in that drawable so add trouble I see such black though it has white in color I should have renamed that file to white and now let's provide in one more attribute that is shows as action and here I will provide two attributes that is always and then we also need to provide this attribute that is collapse action view it is necessary for your action bar to collapse when the search view is toggled and now finally we need to provide in a class name for this that is the action view class name so action view class and this class at the very top here on the pop-up menu as you can see that is the Android X dot app compat dot widget dot search view you should be using this class and no other class so let's select that and that's all for the main menu not XML file so now let me apply this main menu to our main activity Road Java so let me close this main menu dot XML and here inside the main activity dot Java let's go at the bottom below the oncreate method and here what I'll do I'll override this method that is the on create options menu in here what I will do I will get the menu inflator dot inflate and here I need to provide in the resource file for the menu so r dot menu dot mean menu and then we need to pass in the menu and we can simply pass in the menu because it is there inside this on create options and now what I want to do I want to get my menu item so menu item item equal to menu dot find item and our item is r dot ID dot action search like this and now what we need to do we need to find our search view so let's use the search view and again note one thing that you should use this Android X dot app compat dot veget one and not this Android what widget one so we will use this one here and then search you equal to item dot get action view like this and let me cast it to search view again by pressing Alt + Enter and now we will add in query text listener on the search view so search view dot sit on query text listener like this and then we can pass in a new listener here so now let us run our application and see if the search item is working and now if we click on the search box here we see that we have a place to search inside this recycler items so now let us click on back here and we are back inside our recyclerview so the search thing is working that is the search menu is working fine so now let's implement the search functionality inside our recycler view so firstly what I am going to do I am going to go to this recycler adapter class so let's make your recycler adapter implements filterable so implements filterable this one here that is the android what widget one and then you need to override some default methods so this is the default method that is the get filter and click on ok so before doing anything inside this gate filter method let me define one more variable here that is the movies list all which will contains a list of all the movies without being filtered so list of strings and I will call it movies list all and at the very same place where your constructor is you will initialize that movies list all so this dot movies list all equal to new ArrayList and here you can simply pass in the movies list that that you get from the constructor itself so this movie list will contain all the items that will be in the movie list and since we are going to change this movie list by filtering so it won't keep a track of all the movies but this movies this tall will keep a track of all the movies that are present inside your recycler view so now let's go to this gate filter method here and here you need to return something that is of type filter so let's return here filter like this we don't have that filter so let's create that filter here so filter and again you should use this one here that is the Android watt widget one so filter filter equal to new filter and let me put a semicolon here now let me zoom in a little bit more so all the logic of filtering goes inside this method here that is the perform filtering method and the publish results method and let me tell you beforehand itself that this perform filtering method it is run on a background thread and this background thread is created automatically you don't have to do anything for that and this publish result method is run on a UI thread that is it runs on a UI thread so now this perform filtering method returns a variable of type filter results so let's go inside that method here and let this be return null for now so here let me create a list of strings that is a list of movies so filtered list and I initialize it right away like this so now what we want to do we want to check if the chart sequence is empty so for that if charge sequence tour to string dot is empty if it is empty then what you want to do you want to simply add the movies that is the all movies inside your filtered list because if it is empty then all the movies should be shown so filtered list dot add all and here you can pass in movies list all like this else so now inside this else block we can look through our list of movies so for a string movie in movies list all because movies list all contain all the movies and then what we want to do we want to check if the movie contains that chart sequence so for that we can use this thing here if movie dot to lowercase because firstly we are converting the movie to the lowercase and then if it contains the char sequence so here you can pass in charge sequence door to string dot to lowercase again because the filtering should not depend on case like this so now if it contains that then we will add that movie to our filtered list so filter list or add and here we can pass in that movie like this and now what we want to do we want to create a variable of type filter results so filter results filter results equal to new filter results and then filtered results not values equal to filtered list that is the list we have created initially at the very start of this perform filtering method and now what you want to do you want to return this thing here that is the filter results here like this and now this return value of this perform filtering method will return this filter results to this publish results method here as you can see here that it takes in a parameter of filter results so here what we want to do we want to update the UI here so that is again very simple to do so we call our movies list and we will clear it and then what we want to do we don't want to add that filtered results list to our movies list so movies list dot a doll and then filter results dot values and then we need to cast it as the type of collection so all day ninja then we have casted it as a collection and then we simply want to modify that the data set has been changed so notify data set changed now everything inside this recycler adapter thing is complete so now let's go to main activity to call this gate filter method so here inside this insiders menu that is on query takes change what you need to do you want to call your recycler adapter not get filter and then you need to use this method that is the filter method here and here you need to pass in URL string that is a search string so that search string is new text and that we can get from here and now let's run this application and if everything is fine your search you might be working here so let's click here so let's click on Ironman we see that we have a list of Iron Man movies now let's cross this and all its search for Captain America so we see that we get our search results back inside our recycler view and we can directly click on this black item here to go back to a recycler view so guys this is all about search view and filtering inside your recycler view so if you have liked the video do hit the like button and if you have the subscribe to our channel to subscribe to my channel and if you want you can support me on patreon dot-com I'll provide a link in the description below so thank you bye bye tratar take care and have [Music]
Info
Channel: Mafia Codes
Views: 117,711
Rating: undefined out of 5
Keywords: filter recyclerview with search view, filter recyclerview android, filter recyclerview, filter recyclerview android kotlin, filter recyclerview using searchview on app bar, filter recyclerview using searchview, filter recyclerview android example, filter recyclerview with edittext, filter recyclerview android editext, filter recyclerview with searchview Kotlin, searchview in android, search in recyclerview, search bar in recyclerview android, search recyclerview android example
Id: CTvzoVtKoJ8
Channel Id: undefined
Length: 11min 54sec (714 seconds)
Published: Sat Aug 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.