This tutorial will give you a hands on experience in using Android Spinner as a drop down menu, passing data using android bundle and showing popup notification using android toast.

We will create an android application that consists of a simple spinner that allows selecting an item from a drop down list. We will display static data in the spinner. Selecting an item from spinner would display a toast message.

To pass data in the form of bundles between activities, we’ll use a button to perform an intent and display the data passed to the next screen.

Android Spinner

Android Spinner is just a drop down list similar to what’s seen in other programming languages such as in HTML pages.

In Android, Spinner is used to select one value from a set of values. In the default state, a spinner shows its currently selected value. Touching the spinner displays a drop down menu with all other available values, from which the user can select a new one.

Android spinner is associated with AdapterView. So we need to set the adapter class with the Spinner.

Android Drop Down List

Following xml file shows the layout of a typical spinner in android which consists of a text label and a spinner element tag.

Following snippet shows how to use a spinner in the activity class.

Let’s develop an application where we pass the selected value from the Spinner onto the next screen using Bundles and display a Toast message of the selected value at the same time.

Android Spinner Example Project Structure

Below image shows the android studio project for spinner example.

android-spinner-bundle-toast-example-project

Let’s start with the layout of the MainActivity class. We just need to add Button to the basic_spinner.xml file.

The layout of the SecondActivity is as follows:

Here is the Android Manifest file.

AndroidManifest.xml

The MainActivity and SecondActivity java classes are defined as follows.

In the above code, we’ve displayed a toast when an item from the spinner dropdown menu is selected. On button click we pass the selected spinner item as a string value to the next activity using android bundle. Then the data is retrieved from the bundle and displayed in a TextView. Quick easy and simple, isn’t it?

The screenshots of the app are shown below. I am running it on one of the emulators.

The first screen shows the drop down list contents when the Spinner is opened.

android-spinner

After an item is selected, Toast notification message appears for some time.

Finally, in the second screen, the selected item from the dropdown list is retrieved using Bundles and displayed in the TextView.

android-bundle

Below is a sample run of our android spinner example application in emulator.

android spinner, android drop down list

That’s all for now, we will look into Android ListView in next post. You can download Android Spinner, Bundle and Toast example project from below link.

Reference: Official Doc

By admin

Leave a Reply