This article is about Android Spinner Searchable With Search Box Example.
In this article, we will create filterable drop down of spinner in android studio.
We can put a search box at the top of the spinner drop down to filter the options.
This feature gives us the same result as we get from the searchview.
Android’s in-built system do not give us chance to put searchview in the spinner in any way.
You may find any library on github regarding using searchview in spinner the you can use them easily.
In today’s article, I will use github library but it is not using searchview.
We just need to use the desired XML version of spinner in layout file.
Rest of the things are very easy and smooth.
I am going to use this github library in this example.
Above line will download all the required classes from repository.
Then we can use this library’s sentences easily.
As you can see that I have taken <com.toptoche…..> instead of classical <spinner> tag.
This new tag will implement a spinner from the github library.
So this spinner will react according to the classes written in the library instead of traditional spinner.
Generally, we define spinner options name in string array variable. We also define this variable in java class only.
But in current scenario, we will add spinner options in strings.xml file.
strings.xml file is created automatically in every android studio project.
You can find this file under res->values directory.
Add the below code lines in strings.xml file.
Traditionally, we create string array in java files but in this case we have define it as above.
We will use this variable “Animals”, in the JAVA file to populate the spinner.
Let us make a last change to make searchable spinner.
Below line is creating a string array variable.
I have written this line before the onCreate() method, so that we can use this variable (options) through out the whole class.
Second line is creating an object of the ArrayAdapter class.
simple_spinner_item from the above code is in-built file to represent the front view of the spinner.
Above line will specify a file to be used to generate a view of drop down.
simple_spinner_dropdown_item is also a system generated file of android structure.
These lines will first set the adapter to our spinner.
Then compiler will set the onItemSelected Listener for the spinner.
Now we can use the options variable anywhere in the class as a string array.
It contains all the spinner options in string format.
When the user selects the option from the spinner, compiler will call the below method.
When user have selects a specific options then compiler will call onItemSelected() method.
If user do not choose any option then compiler will call onNothingSelected() method.
onItemSelected() method will simply show a toast that will say that which value is selected by the user.
Thus, we have make searchable spinner with very little coding lines.
That is the only purpose of DemoNuts to create quality apps with efficient and time saving code structure.