Android Spinner Example Source Code

Android Spinner helps us to select an item from the drop down menu. In this post let us see an example on how to populate static values in the spinner drop down.

1. First create a new android project.
2. Under "res" folder you can see string.xml file. Add spinner title in this as illustrated in the below example.

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">AndroidSpinner</string>
    <string name="spinner_title">Select</string>
</resources>

3. Now design a simple layout for spinner with a textview as shown below.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <!-- Text Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="Players:"
        android:layout_marginBottom="5dp"
    />

    <!-- Spinner Element -->
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/spinner_title"
    />
</LinearLayout>

4. Now let us see the key part, the Main Activity for Android Spinner below.

Here once the item is selected from the spinner drop down, the alert message toast gets displayed with the value what you have selected.

AndroidSpinnerActivity.java

package com.androidspinner;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class AndroidSpinnerActivity extends Activity implements OnItemSelectedListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Spinner Element
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        //On Spinner Item click listener
        spinner.setOnItemSelectedListener(this);

        // Spinner Values
        List<String> categories = new ArrayList<String>();
        categories.add("Sachin");
        categories.add("Dhoni");
        categories.add("Shewag");
        categories.add("Virat");
        categories.add("Raina");
        categories.add("Dravid");

        // Creating an adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

        // Spinner Style as a List view with radio buttons
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // On select spinner item
        String item = parent.getItemAtPosition(position).toString();

        // Displaying selected spinner item
        Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();

    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}

5. Call the class name in the activity of your Manifest.xml file and just run it and enjoy playing with spinners and that's it.

No comments:

Post a Comment

My Profile