Android Android Java

Android Spinner (Drop Down List) Example (XML and Java)5 min read

A dropdown or pull-down menu, also known as spinner, is one of the most essential UI elements for an app. In this tutorial I’ll tell you how to add one to your Android app using Java.

In this tutorial, we show you how to do the following tasks :

  1. Render a Spinner in XML, and load the selection items via XML file also.
  2. Render another Spinner in XML, and load the selection items via code dynamically.
  3. Attach a listener on Spinner, fire when user select a value in Spinner.
  4. Render and attach a listener on a normal button, fire when user click on it, and it will display selected value of Spinner.




DEMO: Android Spinner (Drop Down List) Example

Step 1: Open “res/values/strings.xml” file, define the list of items that will display in Spinner (dropdown list).

File : res/values/strings.xml

Step 2: Open “res/layout/acticity_main.xml” file, add two spinner components and a button.

  1. In spinner1, the android:entries represents the selection items in spinner.
  2. In spinner2, the selection items will be defined in code later.

File : res/layout/activity_main.xml

Step 3: Read the code and also code’s comment, it should be self-explanatory.

File : MainActivity.java

Step 4: Create Custom java class.

File : CustomOnItemSelectedListener.java

Android Spinner (Drop Down List) Example (XML and Java)
Android Spinner (Drop Down List) Example (XML and Java)

Leave a Comment