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 …

android spinner with search

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.

[sociallocker id=1372] Android Spinner with Search Example [/sociallocker]

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

Hello, I am Manish Kumar. I am a B.Tech Student at NIT Jamshedpur. Fortunately, I find myself quite passionate about Computers and Technology. Android is my most recent Crush.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x