AI2 Component  (Version nb184)
Spinner.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2018 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
9 import android.os.Build.VERSION;
10 import android.os.Build.VERSION_CODES;
11 import android.view.View;
12 import android.widget.AdapterView;
13 import android.widget.AdapterView.OnItemSelectedListener;
14 import android.widget.ArrayAdapter;
15 
29 
39 @DesignerComponent(version = YaVersion.SPINNER_COMPONENT_VERSION,
40  description = "<p>A spinner component that displays a pop-up with a list of elements." +
41  " These elements can be set in the Designer or Blocks Editor by setting the" +
42  "<code>ElementsFromString</code> property to a string-separated concatenation" +
43  " (for example, <em>choice 1, choice 2, choice 3</em>) or by setting the " +
44  "<code>Elements</code> property to a List in the Blocks editor. " +
45  "Spinners are created with the first item already selected. So selecting " +
46  " it does not generate an After Picking event. Consequently it's useful to make the " +
47  " first Spinner item be a non-choice like \"Select from below...\". </p>",
48  category = ComponentCategory.USERINTERFACE,
49  nonVisible = false,
50  iconName = "images/spinner.png")
51 @SimpleObject
52 public final class Spinner extends AndroidViewComponent implements OnItemSelectedListener {
53 
54  private final android.widget.Spinner view;
55  private ArrayAdapter<String> adapter;
56  private YailList items = new YailList();
57  private int oldAdapterCount;
58  private int oldSelectionIndex;
59 
61  super(container);
62  // Themes made the Spinner look and feel change significantly. This allows us to be backward
63  // compatible with established expectations of behavior. However, on Honeycomb and higher there
64  // is a different constructor to get the old behavior. To ensure we work on older devices, that
65  // instantiation is moved to HoneycombUtil
66  if (VERSION.SDK_INT < VERSION_CODES.HONEYCOMB) {
67  view = new android.widget.Spinner(container.$context());
68  } else {
70  }
71 
72  // set regular and dropdown layouts
73  adapter = new ArrayAdapter<String>(container.$context(), android.R.layout.simple_spinner_item);
74  adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
75  view.setAdapter(adapter);
76  view.setOnItemSelectedListener(this);
77 
78  container.$add(this);
79 
80  Prompt("");
81  oldSelectionIndex = SelectionIndex();
82  }
83 
84  @Override
85  public View getView(){
86  return view;
87  }
88 
92  @SimpleProperty(description = "Returns the current selected item in the spinner ",
93  category = PropertyCategory.BEHAVIOR)
94  public String Selection(){
95  return SelectionIndex() == 0 ? "" : (String) view.getItemAtPosition(SelectionIndex() - 1);
96  }
97 
101  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_STRING, defaultValue = "")
102  @SimpleProperty(description = "Set the selected item in the spinner",
103  category = PropertyCategory.BEHAVIOR)
104  public void Selection(String value){
106  }
107 
111  @SimpleProperty(description = "The index of the currently selected item, starting at 1. If no " +
112  "item is selected, the value will be 0.", category = PropertyCategory.BEHAVIOR)
113  public int SelectionIndex(){
114  return ElementsUtil.selectionIndex(view.getSelectedItemPosition() + 1, items);
115  }
116 
123  @SimpleProperty(description = "Set the spinner selection to the element at the given index." +
124  "If an attempt is made to set this to a number less than 1 or greater than the number of " +
125  "items in the Spinner, SelectionIndex will be set to 0, and Selection will be set to empty.",
126  category = PropertyCategory.BEHAVIOR)
127  public void SelectionIndex(int index){
128  oldSelectionIndex = SelectionIndex();
129  view.setSelection(ElementsUtil.selectionIndex(index, items) - 1); // AI lists are 1-based
130  }
131 
135  @SimpleProperty(description = "returns a list of text elements to be picked from.",
136  category = PropertyCategory.BEHAVIOR)
137  public YailList Elements(){
138  return items;
139  }
140 
144  @SimpleProperty(description = "Adds the passed text element to the Spinner list",
145  category = PropertyCategory.BEHAVIOR)
146  public void Elements(YailList itemList){
147  // The following conditional handles special cases for the fact that
148  // spinners automatically select an item when non-empty data is fed
149  if (itemList.size() == 0) {
150  SelectionIndex(0);
151  } else if (itemList.size() < items.size() && SelectionIndex() == items.size()) {
152  SelectionIndex(itemList.size());
153  }
154  items = ElementsUtil.elements(itemList, "Spinner");
155  setAdapterData(itemList.toStringArray());
156  }
157 
162  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_TEXTAREA, defaultValue = "")
163  @SimpleProperty(description = "Sets the Spinner list to the elements passed in the " +
164  "comma-separated string", category = PropertyCategory.BEHAVIOR)
165  public void ElementsFromString(String itemstring){
167  }
168 
169  private void setAdapterData(String[] theItems) {
170  oldAdapterCount = adapter.getCount();
171  adapter.clear();
172  for (int i = 0; i < theItems.length; i++){
173  adapter.add(theItems[i]);
174  }
175  }
176 
180  @SimpleProperty(description = "Text with the current title for the Spinner window",
181  category = PropertyCategory.APPEARANCE)
182  public String Prompt(){
183  return view.getPrompt().toString();
184  }
185 
189  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_STRING, defaultValue = "")
190  @SimpleProperty(description = "Sets the Spinner window prompt to the given title",
191  category = PropertyCategory.APPEARANCE)
192  public void Prompt(String str){
193  view.setPrompt(str);
194  }
195 
196  @SimpleFunction(description = "Displays the dropdown list for selection, " +
197  "same action as when the user clicks on the spinner.")
198  public void DisplayDropdown(){
199  view.performClick();
200  }
201 
205  @SimpleEvent(description = "Event called after the user selects an item from the dropdown list.")
206  public void AfterSelecting(String selection){
207  EventDispatcher.dispatchEvent(this, "AfterSelecting", selection);
208  }
209 
210  public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
211  // special case 1:
212  // onItemSelected is fired when the adapter goes from empty to non-empty AND
213  // SelectionIndex was not set, i.e. oldSelectionIndex == 0
214  // special case 2:
215  // onItemSelected is fired when the adapter goes from larger size to smaller size AND
216  // the old selection position (one-based) is larger than the size of the new adapter
217  if (oldAdapterCount == 0 && adapter.getCount() > 0 && oldSelectionIndex == 0 ||
218  oldAdapterCount > adapter.getCount() && oldSelectionIndex > adapter.getCount()) {
219  SelectionIndex(position + 1); // AI lists are 1-based
220  oldAdapterCount = adapter.getCount();
221  } else {
222  SelectionIndex(position + 1); // AI lists are 1-based
224  }
225  }
226 
227  public void onNothingSelected(AdapterView<?> parent){
228  view.setSelection(0);
229  }
230 
231 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.util.YailList
Definition: YailList.java:26
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.Spinner.AfterSelecting
void AfterSelecting(String selection)
Definition: Spinner.java:206
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_STRING
static final String PROPERTY_TYPE_STRING
Definition: PropertyTypeConstants.java:237
com.google.appinventor.components
com.google.appinventor.components.runtime.Spinner
Definition: Spinner.java:52
com.google.appinventor.components.runtime.util.ElementsUtil.selectionIndex
static int selectionIndex(int index, YailList items)
Definition: ElementsUtil.java:45
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.ComponentContainer.$add
void $add(AndroidViewComponent component)
com.google.appinventor.components.runtime.Spinner.DisplayDropdown
void DisplayDropdown()
Definition: Spinner.java:198
com.google.appinventor.components.runtime.util.ElementsUtil.setSelectedIndexFromValue
static int setSelectedIndexFromValue(String value, YailList items)
Definition: ElementsUtil.java:60
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_TEXTAREA
static final String PROPERTY_TYPE_TEXTAREA
Definition: PropertyTypeConstants.java:248
com.google.appinventor.components.runtime.Spinner.ElementsFromString
void ElementsFromString(String itemstring)
Definition: Spinner.java:165
com.google.appinventor.components.runtime.util.ElementsUtil.elements
static YailList elements(YailList itemList, String componentName)
Definition: ElementsUtil.java:33
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.Spinner.Prompt
String Prompt()
Definition: Spinner.java:182
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.util.ElementsUtil.elementsFromString
static YailList elementsFromString(String itemString)
Definition: ElementsUtil.java:17
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.runtime.Spinner.Selection
String Selection()
Definition: Spinner.java:94
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.Spinner.getView
View getView()
Definition: Spinner.java:85
com.google.appinventor.components.runtime.Spinner.Elements
YailList Elements()
Definition: Spinner.java:137
com.google.appinventor.components.runtime.util.HoneycombUtil.makeSpinner
static Spinner makeSpinner(Context activity)
Definition: HoneycombUtil.java:41
com.google.appinventor.components.runtime.Spinner.Spinner
Spinner(ComponentContainer container)
Definition: Spinner.java:60
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.AndroidViewComponent.container
final ComponentContainer container
Definition: AndroidViewComponent.java:29
com.google.appinventor.components.runtime.Spinner.SelectionIndex
int SelectionIndex()
Definition: Spinner.java:113
com.google
com
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.Spinner.onItemSelected
void onItemSelected(AdapterView<?> parent, View view, int position, long id)
Definition: Spinner.java:210
com.google.appinventor.components.runtime.AndroidViewComponent
Definition: AndroidViewComponent.java:27
com.google.appinventor.components.annotations.PropertyCategory.APPEARANCE
APPEARANCE
Definition: PropertyCategory.java:16
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.runtime.Spinner.onNothingSelected
void onNothingSelected(AdapterView<?> parent)
Definition: Spinner.java:227
com.google.appinventor.components.annotations
com.google.appinventor.components.runtime.util.ElementsUtil
Definition: ElementsUtil.java:15
com.google.appinventor.components.runtime.util.YailList.size
int size()
Definition: YailList.java:172
com.google.appinventor
com.google.appinventor.components.runtime.util.HoneycombUtil
Definition: HoneycombUtil.java:18