Android Spinner (Drop Down)

 

In general Android Spinner are famous by the name Drop-down. Android Spinner provides a way to enlist multiple items and allow user to select one item out of them. Apart from normal UI Android Spinners can be customised in several ways which will also be covered in this tutorial.

 


 

DownloadSource Code : Spinner.zip


 

Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) Spinner which you might have downloaded from above or you can follow the scripts below by copying it to implement in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/Spinner/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.

 

 

1. activity_main.xml : Layout file

  •  We have simply added a spinner inside Relative Layout.

 

 

2. MainActivity.java

Explaining above Code :

  • Line 16 & 23 We have declared spinner and linked it with that of (activity_main.xml) Layout file.
  • Line 27 We make a ArrayList which will hold all the option-items for our Spinner.
  • Line 28-35 Next we add items on-by-one to this arrayList. (While using this arraylist with DB you will just have to repeat this process inside loop). Remember that over here we are just populating arrayList, our spinner is still empty.
  • Line 56  ArrayAdapter are used to add values from our ArrayList to Spinners and to customize our Spinner. You will see 3 parameters for ArrayList on right side. 1st parameter is context, 2nd is to customize look of our spinner & 3rd is data source i.e. our ArrayList. (You will see comment from Line 40-52 replace them one-by-one with this line to see various themes for spinner )
  • Line 57 We associate this arrayAdapter with our spinner declared. So now our spinner is populated with values.
  • Line 64-75 This block adds itemSelectListener to our spinner, inside this the method onItemSelected gives you the position and  value for selected item.

 

 

Final Output :


Android Spinner

 

Do It Yourself :

Try various custom views for the spinner using ArrayAdapter at Line-56 which using commented line-41-50

 

 

Report Errors + Bugs & Become Insider for Nestedif.com

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to insider@nestedif.com you could help other peers.