Android Custom Spinner Tutorial

Spinner is a drop-down menu that displays multiple items to be able user to select an item from the menu. After user select item, the spinner will show the selected item on the device screen. It is much useful when you have multiple items and only allow the user to select one item on it. There have many ways to populate the list in the spinner. In this tutorial, I will guide you how to make android custom spinner in your android project.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “SpinnerExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Create a new Custom Spinner Layout

Right-click your layout folder from the res package, and right click creates a new XML “custom_spinner_items”. After that edit this file to the following source code.

Create a new Adapter spinner

Right-click your package name and create a new class for your spinner adapter name”CustomSpinnerAdapter”. Follow the sample source code below.

Edit activity_main.xml layout

Go to this layout and add spinner in the relative layout.

Edit MainActivity.java class

Go to MainActivity class, this class will populate the content to the customSpinnerAdapter and display to the screen.

Add new drawable file

Create new XML file”custom_spinner_background” in your drawable, this file is to customize your spinner background.

Run Your Project

In conclusion, now you can run this project in your device.

(Android Custom Spinner)

Source Code

(Visited 1,249 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

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