Custom spinner tutorial for Android

Why you need the custom spinner?

You need the custom spinner, if you want to add Images to the spinner or set custom properties like Textual appearance, Background etc. Spinner widget in android consists of two views one is the dropdown view and another one is selected view. You can customize these two views as per your requirement. For modifying the dropdown view, you need to override the following method from BaseAdapter class.

For modifying selected view, you need to override the following method from BaseAdapter class.

Creating a project

Create a new android studio project, create a default activity.

Working with an example

  • Download images required for this project.
  • Copy all the downloaded images in res>drawable folder.
  • Right click on res>layout and then click New>Layout resource file. Enter custom_spinner_row as the file name. Add the following code to custom_spinner_row.xml
  • Right click on app>java>your package name, then click on New>Java Class. Enter Item as the class name. Add the following code to Item.java
  • Right click on app>java>your package name, then click on New>Java Class. Enter CustomSpinnerAdapter as the class name. In your CustomSpinnerAdapter.java, add the following code.
  • Modify your activity_main.xml file to add the following code.
  • In your MainActivity.java file, add the following code.

Output

Custom spinner(dropdown) with image and text android
Custom spinner (dropdown) android

Download the source code.

References

Leave a Reply

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