A Spinner is a widget similar to a drop-down list.
In this tutorial, you will create a simple spinner component for displaying the planet list. When you select an item in the list, a message indicating the toast option is displayed. The procedure is as follows:
1. Create a new project named HelloSpinner.
2. Open the res/layout/main. xml file and insert the following content into it:
<?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"> <TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="@string/planet_prompt" /> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:prompt="@string/planet_prompt" /></LinearLayout>
Note that the android: text attribute of TextView and the android: prompt attribute of Spinner both reference the same string resource. This string is used as the title of the spinner. When it is applied to the Spinner, the string text will appear at the top of the current list.
3. Create a stings. xml file in the res/values directory and edit it as follows:
<?xml version="1.0" encoding="utf-8"?><resources><string name="planet_prompt">Choose a planet</string><string-array name="planets_array"><item>Mercury</item><item>Venus</item><item>Earth</item><item>Mars</item><item>Jupiter</item><item>Saturn</item><item>Uranus</item><item>Neptune</item><string-array></resources>
<String> the source defines the title string referenced by both TextView and Spinner. The <string-array> element defines the list of strings to be listed in the Spinner.
4. Open the HelloSpinner. java file and insert the following code into the onCreate () method:
@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.planets_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter);}
After setting the main. xml file as the content view, you can use findViewById (int) to find the Spinner component. Then, the createFromResource () method creates a new ArrayAdapter, which binds each item in the string array used for the initial Spinner list. R. array. planets_array ID references the string_array defined above, while android. R. layout. simple_spinner_item references a standard spinner style (appearance) defined by the system ). Then call setDropDownViewResource (int) to define the appearance (simple_spinner_dropdown_item) of each item when the spinner is opened ). Finally, by calling setAdapter (T), ArrayAdapter is associated with all its items.
5. Create an internal class by implementing AdapterView. OnItemSelectedListener. This function provides a callback method when items in the Spinner are selected. The following is the internal class:
public class MyOnItemSelectedListener implements OnItemSelectedListener {public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {Toast.makeText(parent.getContext(), "The planet is " +parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); }public void onNothingSelected(AdapterView<?> parent) {// Do nothing. }}
This AdapterView. OnItemSelectedListerer requires the onItemSelected () and onNothingSelected () callback methods. The previous callback method will be called when the item in AdapterView is selected. In this case, a Toast message will pop up, the subsequent callback method will be called when a selection disappears from the AdapterView, which is not considered in this example.
6. Then, apply the MyOnItemSelectedListener to the Spinner by adding the following code to the onCreate () method:
spinner.setOnItemSelectedListerner(new MyOnItemSelectedListener() );
In this way, an anonymous internal class of MyOnItemSelectedListener is created and set as a listener of the Spinner.
7. Run the program. The following result is displayed: