• Android spinner example using ArrayAdapter

    24 February 2015

    Android spinner example using ArrayAdapter and XML resource

    Android spinner is very much like the HTML<select> tag. It basically displays dropdown list and you can select one from the list. There is no multi select available at this time. In this tutorial we will go thru the following ... 

    • Create a spinner from XML resource array
    • Create a spinner dynamically using String Array and ArrayAdapter
    • Ability to capture spinner selection on change using OnItemSelectedListener

    Android spinner from ArrayAdapter string array and from xml resource


    Android spinner from ArrayAdapter string array and from xml resource


    Android spinner from ArrayAdapter string array and from xml resource


    Android spinner from ArrayAdapter string array and from xml resource


    Android spinner from ArrayAdapter string array and from xml resource

    To create a spinner from XML resource we need to define an array in the strings.xml

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    <?xml version="1.0" encoding="utf-8"?>

    <resources>

    <string name="app_name">Android Spinner Example</string>

    <string name="country_prompt">Choose a Nordic Country</string>

    <string-array name="country_array">

    <item>Denmark</item>

    <item>Faroe Islands</item>

    <item>Finland</item>

    <item>Iceland</item>

    <item>Norway</item>

    <item>Svalbard and Jan Mayen</item>

    <item>Sweden</item>

    </string-array>

    <string name="animal_prompt">Choose your favorite animal</string>

    </resources>


    Layout for the screen - main.xml

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout android:orientation="vertical"

    android:layout_width="fill_parent" android:layout_height="fill_parent"

    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView android:textAppearance="?android:attr/textAppearanceLarge"

    android:id="@+id/textView1" android:layout_height="wrap_content"

    android:layout_width="wrap_content" android:text="@string/country_prompt" />

    <Spinner android:id="@+id/spinner" android:layout_width="match_parent"

    android:layout_height="wrap_content" android:prompt="@string/country_prompt"

    android:entries="@array/country_array" />

    <TextView android:textAppearance="?android:attr/textAppearanceLarge"

    android:id="@+id/textView2" android:layout_height="wrap_content"

    android:layout_width="wrap_content" android:text="@string/animal_prompt" />

    <Spinner android:id="@+id/spinner1" android:layout_width="match_parent"

    android:layout_height="wrap_content" android:prompt="@string/animal_prompt" />

    </LinearLayout>


    Source for the android activity - AndroidSpinnerActivity.java

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    package com.as400samplecode;

    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.AdapterView.OnItemSelectedListener;

    import android.widget.ArrayAdapter;

    import android.widget.Spinner;

    import android.widget.Toast;

    public class AndroidSpinnerActivity extends Activity {

    private String selectedCountry = null;

    private String selectedAnimal = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    //get reference to the spinner from the XML layout

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

    //attach the listener to the spinner

    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

    //Dynamically generate a spinner data

    createSpinnerDropDown();

    }

    //Add animals into spinner dynamically

    private void createSpinnerDropDown() {

    //get reference to the spinner from the XML layout

    Spinner spinner = (Spinner) findViewById(R.id.spinner1);

    //Array list of animals to display in the spinner

    List<String> list = new ArrayList<String>();

    list.add("Bear");

    list.add("Camel");

    list.add("Cat");

    list.add("Cat");

    list.add("Deer");

    list.add("Dog");

    list.add("Goat");

    list.add("Horse");

    //create an ArrayAdaptar from the String Array

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,

    android.R.layout.simple_spinner_item, list);

    //set the view for the Drop down list

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    //set the ArrayAdapter to the spinner

    spinner.setAdapter(dataAdapter);

    //attach the listener to the spinner

    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

    }

    public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

    String selectedItem = parent.getItemAtPosition(pos).toString();

    //check which spinner triggered the listener

    switch (parent.getId()) {

    //country spinner

    case R.id.spinner:

    //make sure the country was already selected during the onCreate

    if(selectedCountry != null){

    Toast.makeText(parent.getContext(), "Country you selected is " + selectedItem,

    Toast.LENGTH_LONG).show();

    }

    selectedCountry = selectedItem;

    break;

    //animal spinner 

    case R.id.spinner1:

    //make sure the animal was already selected during the onCreate

    if(selectedAnimal != null){

    Toast.makeText(parent.getContext(), "Animal selected is " + selectedItem,

    Toast.LENGTH_LONG).show();

    selectedAnimal = selectedItem;

    break;

    }

    }

    public void onNothingSelected(AdapterView<?> parent) {

    // Do nothing.

    }

    }

    }


    AndroidManifest.xml file

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    <?xml version="1.0" encoding="utf-8"?>

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.as400samplecode"

    android:versionCode="1"

    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name"

    android:theme="@android:style/Theme.Light">

    <activity android:name=".AndroidSpinnerActivity"

    android:label="@string/app_name">

    <intent-filter>

    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />

    </intent-filter>

    </activity>

    </application>

    </manifest>


Comments

Comments closed on this post.