Android Spinner - Androhub

Spinner

Android Spinner

Spinner is a widget similar to a drop-down list for selecting items.

Example

In this example I will show you how to implement Simple and Custom Spinner in Android.

VIDEO DEMO

Let’s get start by creating a project in Eclipse IDE.

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)

2.  Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout i used two Spinner, one for simple and one for custom.

3. For custom Spinner we have to make a new layout for custom view naming custom_spinner_layout.xml under layout folder. In this layout i had just taken an ImageView and a TextView.

4. Now create a new class naming SpinnerData_Items.java  for getter and setter for custom view.

5. Now make a custom adapter class naming CustomSpinner_Adapter.java  for inflating custom view and add the following code.

6. Finally, come to MainActivity.java and add the following code. In this class we load both the spinners (Simple and Custom) with some data and set adapter to both the spinners. And a toast will be shown when the item is selected from spinner.

7. Now, run the app and you will get the output as shown in video.

Thanks. :)

 

Post comment

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