1 page.title=Hello, Spinner
2 parent.title=Hello, Views
6 <p>A {@link android.widget.Spinner} is a widget that allows the user to select an item from a group.
7 It is similar to a dropdown list and will allow scrolling when the
8 list exceeds the available vertical space on the screen.</p>
12 <li>Start a new project/Activity called HelloSpinner.</li>
13 <li>Open the layout file.
16 <?xml version="1.0" encoding="utf-8"?>
17 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18 android:orientation="vertical"
19 android:padding="10dip"
20 android:layout_width="fill_parent"
21 android:layout_height="wrap_content">
24 android:layout_width="fill_parent"
25 android:layout_height="wrap_content"
26 android:layout_marginTop="10dip"
27 android:text="Please select a planet:"
31 android:id="@+id/spinner"
32 android:layout_width="fill_parent"
33 android:layout_height="wrap_content"
34 android:drawSelectorOnTop="true"
35 android:prompt="@string/planet_prompt"
40 <p>Notice that the Spinner's <code>android:prompt</code> is a string resource. In
41 this case, Android does not allow it to be a string, it must be a reference to a resource.
45 <li>Open the strings.xml file in res/values/ and add the following <code><string></code>
46 element inside the <code><resources></code> element:
48 <string name="planet_prompt">Choose a planet</string>
52 <li>Create a new XML file in res/values/ called arrays.xml. Insert the following:
56 <string-array name="planets">
57 <item>Mercury</item>
58 <item>Venus</item>
59 <item>Earth</item>
60 <item>Mars</item>
61 <item>Jupiter</item>
62 <item>Saturn</item>
63 <item>Uranus</item>
64 <item>Neptune</item>
69 <p>This is the list of items (planets) that the user can select from in the Spinner widget.</p>
72 <li>Now open the HelloSpinner.java file. Insert the following code into the HelloSpinner class:
75 public void onCreate(Bundle savedInstanceState) {
76 super.onCreate(savedInstanceState);
77 setContentView(R.layout.main);
79 Spinner s = (Spinner) findViewById(R.id.spinner);
80 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
81 this, R.array.planets, android.R.layout.simple_spinner_item);
82 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
83 s.setAdapter(adapter);
86 <p>That's it. We start by creating a Spinner from our layout. We then create an {@link android.widget.ArrayAdapter}
87 that binds each element of our string array to a layout view—we pass <code>createFromResource</code> our Context,
88 the array of selectable items and the type of layout we'd like each one bound to. We then call
89 <code>setDropDownViewResource()</code> to define the type of layout in which to present the
90 entire collection. Finally, we set this Adapter to associate with our Spinner,
91 so the string items have a place to go.</p>
96 <p>It should look like this:</p>
97 <img src="images/hello-spinner.png" width="150px" />
102 <li>{@link android.R.layout}</li>
103 <li>{@link android.widget.ArrayAdapter}</li>
104 <li>{@link android.widget.Spinner}</li>