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
To create a spinner from XML resource we need to define an array in the strings.xml
<?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
<?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
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
<?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>
Recommended Reading
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.