| page.title=Hello, Spinner |
| @jd:body |
| |
| <p>A {@link android.widget.Spinner} is a widget that allows the user to select an item from a group. |
| It is similar to a dropdown list and will allow scrolling when the |
| list exceeds the available vertical space on the screen.</p> |
| |
| |
| <ol> |
| <li>Start a new project/Activity called HelloSpinner.</li> |
| <li>Open the layout file. |
| Make it like so: |
| <pre> |
| <?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"> |
| |
| <TextView |
| android:layout_width="fill_parent" |
| android:layout_height="wrap_content" |
| android:layout_marginTop="10dip" |
| android:text="Please select a planet:" |
| /> |
| |
| <Spinner |
| android:id="@+id/spinner" |
| android:layout_width="fill_parent" |
| android:layout_height="wrap_content" |
| android:drawSelectorOnTop="true" |
| android:prompt="@string/planet_prompt" |
| /> |
| |
| </LinearLayout> |
| </pre> |
| <p>Notice that the Spinner's <code>android:prompt</code> is a string resource. In |
| this case, Android does not allow it to be a string, it must be a reference to a resource. |
| So...</p> |
| </li> |
| |
| <li>Open the strings.xml file in res/values/ and add the following <code><string></code> |
| element inside the <code><resources></code> element: |
| <pre> |
| <string name="planet_prompt">Choose a planet</string> |
| </pre> |
| </li> |
| |
| <li>Create a new XML file in res/values/ called arrays.xml. Insert the following: |
| <pre> |
| <resources> |
| |
| <string-array name="planets"> |
| <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> |
| </pre> |
| <p>This is the list of items (planets) that the user can select from in the Spinner widget.</p> |
| </li> |
| |
| <li>Now open the HelloSpinner.java file. Insert the following code into the HelloSpinner class: |
| <pre> |
| @Override |
| public void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| setContentView(R.layout.main); |
| |
| Spinner s = (Spinner) findViewById(R.id.spinner); |
| ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( |
| this, R.array.planets, android.R.layout.simple_spinner_item); |
| adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
| s.setAdapter(adapter); |
| } |
| </pre> |
| <p>That's it. We start by creating a Spinner from our layout. We then create an {@link android.widget.ArrayAdapter} |
| that binds each element of our string array to a layout view—we pass <code>createFromResource</code> our Context, |
| the array of selectable items and the type of layout we'd like each one bound to. We then call |
| <code>setDropDownViewResource()</code> to define the type of layout in which to present the |
| entire collection. Finally, we set this Adapter to associate with our Spinner, |
| so the string items have a place to go.</p> |
| </li> |
| |
| <li>Now run it.</li> |
| </ol> |
| <p>It should look like this:</p> |
| <img src="images/hello-spinner.png" width="150px" /> |
| |
| |
| <h3>Resources</h3> |
| <ul> |
| <li>{@link android.R.layout}</li> |
| <li>{@link android.widget.ArrayAdapter}</li> |
| <li>{@link android.widget.Spinner}</li> |
| </ul> |
| |
| <p><a href="{@docRoot}guide/tutorials/views/hello-views-index.html">← Back to Hello Views</a></p> |