1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.view;
18 
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 import com.example.android.apis.R;
22 
23 import android.app.Activity;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.widget.AdapterView;
27 import android.widget.ArrayAdapter;
28 import android.widget.Spinner;
29 import android.widget.Toast;
30 import android.widget.AdapterView.OnItemSelectedListener;
31 
32 
33 public class Spinner1 extends Activity {
34 
showToast(CharSequence msg)35     void showToast(CharSequence msg) {
36         Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
37     }
38 
39     @Override
onCreate(Bundle savedInstanceState)40     public void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         setContentView(R.layout.spinner_1);
43 
44         Spinner s1 = (Spinner) findViewById(R.id.spinner1);
45         ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
46                 this, R.array.colors, android.R.layout.simple_spinner_item);
47         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
48         s1.setAdapter(adapter);
49         s1.setOnItemSelectedListener(
50                 new OnItemSelectedListener() {
51                     public void onItemSelected(
52                             AdapterView<?> parent, View view, int position, long id) {
53                         showToast("Spinner1: position=" + position + " id=" + id);
54                     }
55 
56                     public void onNothingSelected(AdapterView<?> parent) {
57                         showToast("Spinner1: unselected");
58                     }
59                 });
60 
61         Spinner s2 = (Spinner) findViewById(R.id.spinner2);
62         adapter = ArrayAdapter.createFromResource(this, R.array.planets,
63                 android.R.layout.simple_spinner_item);
64         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
65         s2.setAdapter(adapter);
66         s2.setOnItemSelectedListener(
67                 new OnItemSelectedListener() {
68                     public void onItemSelected(
69                             AdapterView<?> parent, View view, int position, long id) {
70                         showToast("Spinner2: position=" + position + " id=" + id);
71                     }
72 
73                     public void onNothingSelected(AdapterView<?> parent) {
74                         showToast("Spinner2: unselected");
75                     }
76                 });
77     }
78 }
79