Android Spinner Tutorial With Example For Beginners | Android Studio

Topic of today is Android Spinner Tutorial With Example For Beginners.

In android development, spinner widget gives us facility to create a drop down menu with multiple options.

User can select any option from this drop down menu and we can track the selection of the user and can write logic with selected option.

Spinner allows user to select only one value. Spinner doe not support multiple selection from the drop down list.

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.

Just like ListView or RecyclerView, spinner is also associated with the Adapter. Hence, we need to attach the adapter class to the spinner to populate it with various options.

Methods to create spinner

Generally, developers create spinner with one of the following method.

  1. Using Array Adapter
  2. Or with the help of the Custom Adapter

When you want to develop very basic spinner with only text options, you should use the first method.

You can use second method when you want to customize your spinner heavily. For example, you want to add images along with the text in the spinner.

Functions Of Spinner

Android provides some predefined methods which helps developers to make spinner within no time.

1. setAdapter(SpinnerAdapter adapter)

Using this method we can set the SpinnerAdapter to our Spinner.

Adapter will provide data to the spinner.

2. setGravity(int gravity)

Describes how the selected item view is positioned.

3. setEnabled(boolean enabled)

Set the enabled state of this view.

4. onTouchEvent(MotionEvent event)

Implement this method to handle touch screen motion events.

5. performClick()

Call this view’s OnClickListener, if it is defined.

6. getDropDownWidth()

Get the configured width of the spinner’s popup window of choices in pixels.

7. getPopupBackground()

Get the background drawable for the spinner’s popup window of choices.

8. setDropDownWidth(int pixels)

Set the width of the spinner’s popup window of choices in pixels.

Example of Spinners

Let us create two examples of spinner in the android studio.

First example will contain spinner with text options. It will be a simple example where we will use array adapter.

In the second example, we will add image along with the text in every drop down option of spinner.

So let’s make first example step by step.

Step 1. XML file writings

First of all, create a new project in android studio.

Now add the following source code in activity_main.xml file

I have taken spinner in the main layout file. Do not forget to give id to the spinner.

Now in the MainActivity.java file, you should write down the below code block

Let us understand the above code deeply.

Compiler will first create the object of the spinner and will associate a id using findViewById() method.

Then below code will create a simple list.

A list of string called “categories” will have the name of the laptop manufacture companies in the string format.

This list will work as the data source to populate our spinner. User will have options from this category list in the drop down menu.

To set this category list in the spinner, we need one adapter.

Now look at the below coding lines

Compiler will create the object of the Array Adapter class. As you can see that it has three parameters.

First parameter is just a context of the application.

Second parameter is asking for the layout which will help to create the UI of the drop down items. I have used default layout in the above code. You can also make custom file when you are using custom adapter (We will create this in second example).

Last parameter contains the data source which is “categories” list.

After this, compiler will set the adapter to the spinner.

Now read the following code structure

Compiler will run the above code when the user selects any option from the spinner.

Android will show one toast when the user selects the option. Toast will have text like “Selected : Dell”

Watch the output in the below video

Download First Example

Click the below link to download the first spinner example source code.

Download Spinner First Source Code

Second Example Of Spinner

In this second project, we will insert image in the drop down option.

Make another new project in the android studio.

Step 1. Layout file and changes

Following is the code structure for activity_main.xml file.

Again, there is only one spinner in the main layout code.

We will create custom base adapter in this example. For this, we need one separate XML file to create view of the drop down items.

So go ahead and make a new XML file under res->layout directory.

Set the name of this file as sp_item.xml and add the following coding lines in it

I have added one image view and one text view in this file. They are under linearlayout with horizontal orientation.

So these both UI widgets will be added side by side.

Every drop down option will contain this image view and text view.

Step 2. Adding Images

Click the following link to download the images.

Download fruit_images 100 100

Add these images inside the res->drawable folder.

Step 3. Adapter For Spinner

Create a new JAVA class and give it a name like SpinnerAdapter.java

Code lines for this is as the following

Read the constructor in the above code block.

Constructor of adapter have three parameters. First parameter will give us context of the application.

Second one giving us the integer array. This integer array holds the reference to the images that we have added in the drawable folder in step 2.

Third parameter have string array. String array contains the name of the fruits in the text format.

We will create both integer array and string array in the MainActivity.java file. Then we will pass them while creating the object of the SpinnerAdapter.class in the MainActivity.java file only.

In the getView() method, compiler will inflate the sp_item.xml method, which we have created in the first step.

Then it will set the text in the text view and an image in the image view.

Step 4. Last Java File

At last, add the following coding lines in MainActivity.java file.

First two lines are creating an integer array and string array.

Then compiler will create the object of the SpinnerAdapter.java class.

Here, it will pass the integer and string array as the second and third parameter respectively.

Following images are the outcome of the example.

 

android custom spinner with image

Last View Toast

android custom spinner with image, android spinner

Drop Down


android custom spinner with image, android spinner

Download Source code

Click the following line to download source code for second spinner example

Download Spinner Image Source Code

Useful Spinner Tutorials

Once you are ready to make basic spinner, you can read following tutorial to enhance your skill in spinner.

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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