Android Spinner with Search using XML Example

Hi there, in this Android Spinner with Search Example  you will learn how to use xml spinner with a search option. As soon as the spinner’s down arrow would be tapped, a searchbar followed by a  list  of  options would pop up. The list will eventually get filtered as you type something into the search bar. The sample output of this Android Spinner with Search can be seen below.

android spinner with search
Android Spinner with Search
That’s what you are gonna get through in this tutorial. So, Let’s get Started.

Android Spinner with Search Example

Creating a New Android Studio Project

  • Create a new Android Studio Project with Empty Activity.
  • As usual, we shall begin with configuring the MainActivity.java and activity_main.xml. Add the following codes into respective files.
  • Here’s the activity_main.xml. It consists of the xml code of the Searchbar.

  • We needn’t configure the MainActivity.java in this project. Instead, we’ll be creating a two classes named SearchableSpinner.java and SearchableListDialog.java. So, go ahead and create a class named SearchableSpinner.java and the following code. In this class, there’s a string called selectedItem which contains the value of the selected item in the searchlist. You can use it as per required.

  • Next, create a class named SearchableListDialog.java and add the following code.

  • By now, we are done with the Java part. Now, we have some business  left with xml resource files. So, let’s deal with it.
  • Create a layout resource file named  searchable_list_dialog.xml and add the following code. It configures the view after spinner is tapped.

  • Next, create a values resource file named colleges.xml and add the following code. It will contain the list of options to be filtered upon the search.

  • Finally, create a values resource file named attrs.xml and add the following code.

  • That’all. Run the app and you will find a spinner. As you tap it, it shall turn into a search bar. If you still find some problem, let me know in the comments section or you can also go through the source code of this Android Spinner with Search Example.

So thats all for this Android Spinner with Search Example. Stay tuned for more android tutorials. Thank You 🙂

7 thoughts on “Android Spinner with Search using XML Example”

  1. Hai sir , nice tutorial and i have been using this project to show our state college details and information. problem is when a user search with a college name using spinner list and click on search item , the respective college details should be opened in new activity . please help with this

    Reply
  2. This works good with done funtionality but whenever if a dilaog is opened and direct close of app button is leading to Crash of app.

    Reply

Leave a Comment