Android Spinner Searchable With Search Box Example

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.

Ending View

Step 1. Defining Dependencies

I am going to use this github library in this example.

So add the following line in your build.gradle(Module:app) file

  • Above line will download all the required classes from repository.
  • Then we can use this library’s sentences easily.

Step 2. Layout

Add the following coding lines in activity_main.xml file

  • 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.

Step 3. Adding Options

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.

Step 4. Final Change

Let us make a last change to make searchable spinner.

Write down the below coding lines in file

Moving Deep in Above code

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.

Now consider the below source code

  • First line is finding the spinner with use of it’s ID from the activity_main.xml file
  • Second line is creating an object of the ArrayAdapter class.
  • This object will get the strings from the string array variable (Animals) of the strings.xml file
  • simple_spinner_item from the above code is in-built file to represent the front view of the spinner.

Read the below coding lines

  • 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.

Let us understand the below lines

  • These lines will first set the adapter to our spinner.
  • Then compiler will set the onItemSelected Listener for the spinner.

Following line will generate string array from the strings.xml file

  • 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.

Download Source Code For Android Spinner Searchable

[sociallocker]Download Source Code For Spinner_SearchBox[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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