blob: 3a04214a1f3d52983f8ceedc8b65b87142acd13b (
plain)
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
99
100
101
102
103
104
105
106
|
page.title=Hello, Spinner
parent.title=Hello, Views
parent.link=index.html
@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>
|