AI2 Component  (Version nb184)
ListPicker.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-2012 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 
22 
23 import android.app.Activity;
24 import android.content.Intent;
25 import android.view.WindowManager;
26 
42 @DesignerComponent(version = YaVersion.LISTPICKER_COMPONENT_VERSION,
43  category = ComponentCategory.USERINTERFACE,
44  description = "<p>A button that, when clicked on, displays a list of " +
45  "texts for the user to choose among. The texts can be specified through " +
46  "the Designer or Blocks Editor by setting the " +
47  "<code>ElementsFromString</code> property to their string-separated " +
48  "concatenation (for example, <em>choice 1, choice 2, choice 3</em>) or " +
49  "by setting the <code>Elements</code> property to a List in the Blocks " +
50  "editor.</p>" +
51  "<p>Setting property ShowFilterBar to true, will make the list searchable. " +
52  "Other properties affect the appearance of the button " +
53  "(<code>TextAlignment</code>, <code>BackgroundColor</code>, etc.) and " +
54  "whether it can be clicked on (<code>Enabled</code>).</p>")
55 @SimpleObject
56 @UsesActivities(activities = {
57  @ActivityElement(name = "com.google.appinventor.components.runtime.ListPickerActivity",
58  configChanges = "orientation|keyboardHidden",
59  screenOrientation = "behind")
60 })
62 
63  private static final String LIST_ACTIVITY_CLASS = ListPickerActivity.class.getName();
64  static final String LIST_ACTIVITY_ARG_NAME = LIST_ACTIVITY_CLASS + ".list";
65  static final String LIST_ACTIVITY_RESULT_NAME = LIST_ACTIVITY_CLASS + ".selection";
66  static final String LIST_ACTIVITY_RESULT_INDEX = LIST_ACTIVITY_CLASS + ".index";
67  static final String LIST_ACTIVITY_ANIM_TYPE = LIST_ACTIVITY_CLASS + ".anim";
68  static final String LIST_ACTIVITY_SHOW_SEARCH_BAR = LIST_ACTIVITY_CLASS + ".search";
69  static final String LIST_ACTIVITY_TITLE = LIST_ACTIVITY_CLASS + ".title";
70  static final String LIST_ACTIVITY_ORIENTATION_TYPE = LIST_ACTIVITY_CLASS + ".orientation";
71  static final String LIST_ACTIVITY_ITEM_TEXT_COLOR = LIST_ACTIVITY_CLASS + ".itemtextcolor";
72  static final String LIST_ACTIVITY_BACKGROUND_COLOR = LIST_ACTIVITY_CLASS + ".backgroundcolor";
73 
74  private YailList items;
75  private String selection;
76  private int selectionIndex;
77  private boolean showFilter =false;
78  private static final boolean DEFAULT_ENABLED = false;
79  private String title = ""; // The Title to display the List Picker with
80  // if left blank, the App Name is used instead
81  private boolean resumedFromListFlag = false; //flag so onResume knows if the resume was triggered by closing the listpicker activity
82 
83  private int itemTextColor;
84  private int itemBackgroundColor;
85  public final static int DEFAULT_ITEM_TEXT_COLOR = Component.COLOR_WHITE;
86  public final static int DEFAULT_ITEM_BACKGROUND_COLOR = Component.COLOR_BLACK;
87 
93  public ListPicker(ComponentContainer container) {
94  super(container);
95  items = new YailList();
96  // initialize selectionIndex which also sets selection
97  SelectionIndex(0);
98  itemTextColor = DEFAULT_ITEM_TEXT_COLOR;
99  itemBackgroundColor = DEFAULT_ITEM_BACKGROUND_COLOR;
100 
101  container.$form().registerForOnResume(this);
102  }
103 
104  @Override
105  public void onResume() {
106  if (resumedFromListFlag) {
107  container.$form().getWindow().setSoftInputMode(
108  WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
109  );
110  resumedFromListFlag = false;
111  }
112  }
113 
120  description = "The selected item. When directly changed by the " +
121  "programmer, the SelectionIndex property is also changed to the first " +
122  "item in the ListPicker with the given value. If the value does not " +
123  "appear, SelectionIndex will be set to 0.",
124  category = PropertyCategory.BEHAVIOR)
125  public String Selection() {
126  return selection;
127  }
128 
135  defaultValue = "")
137  public void Selection(String value) {
138  selection = value;
139  // Now, we need to change SelectionIndex to correspond to Selection.
140  selectionIndex = ElementsUtil.setSelectedIndexFromValue(value, items);
141  }
142 
145  defaultValue = DEFAULT_ENABLED ? "True" : "False")
147  public void ShowFilterBar(boolean showFilter) {
148  this.showFilter = showFilter;
149  }
150 
155  description = "Returns current state of ShowFilterBar indicating if " +
156  "Search Filter Bar will be displayed on ListPicker or not")
157  public boolean ShowFilterBar() {
158  return showFilter;
159  }
160 
162  defaultValue = Component.DEFAULT_VALUE_COLOR_WHITE)
164  public void ItemTextColor(int argb) {
165  this.itemTextColor = argb;
166  }
167 
168  @SimpleProperty(description = "The text color of the ListPicker items.",
169  category = PropertyCategory.APPEARANCE)
170  @IsColor
171  public int ItemTextColor() {
172  return this.itemTextColor;
173  }
174 
176  defaultValue = Component.DEFAULT_VALUE_COLOR_BLACK)
178  public void ItemBackgroundColor(int argb) {
179  this.itemBackgroundColor = argb;
180  }
181 
185  @SimpleProperty(description = "The background color of the ListPicker items.",
186  category = PropertyCategory.APPEARANCE)
187  @IsColor
188  public int ItemBackgroundColor() {
189  return this.itemBackgroundColor;
190  }
191 
196  description = "The index of the currently selected item, starting at " +
197  "1. If no item is selected, the value will be 0. If an attempt is " +
198  "made to set this to a number less than 1 or greater than the number " +
199  "of items in the ListPicker, SelectionIndex will be set to 0, and " +
200  "Selection will be set to the empty text.",
201  category = PropertyCategory.BEHAVIOR)
202  public int SelectionIndex() {
203  return selectionIndex;
204  }
205 
209  // Not a designer property, since this could lead to unpredictable
210  // results if Selection is set to an incompatible value.
212  public void SelectionIndex(int index) {
213  selectionIndex = ElementsUtil.selectionIndex(index, items);
214  // Now, we need to change Selection to correspond to SelectionIndex.
215  selection = ElementsUtil.setSelectionFromIndex(index, items);
216  }
217 
224  public YailList Elements() {
225  return items;
226  }
227 
235  // TODO(user): we need a designer property for lists
237  public void Elements(YailList itemList) {
238  items = ElementsUtil.elements(itemList, "ListPicker");
239  }
240 
248  defaultValue = "")
249  // TODO(sharon): it might be nice to have a list editorType where the developer
250  // could directly enter a list of strings (e.g. one per row) and we could
251  // avoid the comma-separated business.
253  public void ElementsFromString(String itemstring) {
254  items = ElementsUtil.elementsFromString(itemstring);
255  }
256 
266  description = "Optional title displayed at the top of the list of choices.")
267  public String Title() {
268  return title;
269  }
270 
279  defaultValue = "")
281  public void Title(String title) {
282  this.title = title;
283  }
284 
285  @Override
286  public Intent getIntent() {
287  Intent intent = new Intent();
288  intent.setClassName(container.$context(), LIST_ACTIVITY_CLASS);
289  intent.putExtra(LIST_ACTIVITY_ARG_NAME, items.toStringArray());
290  intent.putExtra(LIST_ACTIVITY_SHOW_SEARCH_BAR, String.valueOf(showFilter)); //convert to string
291  if (!title.equals("")) {
292  intent.putExtra(LIST_ACTIVITY_TITLE, title);
293  }
294  // Get the current Form's opening transition anim type,
295  // and pass it to the list picker activity. For consistency,
296  // the closing animation will be the same (but in reverse)
297  String openAnim = container.$form().getOpenAnimType();
298  intent.putExtra(LIST_ACTIVITY_ANIM_TYPE, openAnim);
299  intent.putExtra(LIST_ACTIVITY_ORIENTATION_TYPE,container.$form().ScreenOrientation());
300  intent.putExtra(LIST_ACTIVITY_ITEM_TEXT_COLOR, itemTextColor);
301  intent.putExtra(LIST_ACTIVITY_BACKGROUND_COLOR, itemBackgroundColor);
302 
303  return intent;
304  }
305 
306  // ActivityResultListener implementation
307 
316  @Override
317  public void resultReturned(int requestCode, int resultCode, Intent data) {
318  if (requestCode == this.requestCode && resultCode == Activity.RESULT_OK) {
319  if (data.hasExtra(LIST_ACTIVITY_RESULT_NAME)) {
320  selection = data.getStringExtra(LIST_ACTIVITY_RESULT_NAME);
321  } else {
322  selection = "";
323  }
324  selectionIndex = data.getIntExtra(LIST_ACTIVITY_RESULT_INDEX, 0);
325  AfterPicking();
326  // It is necessary for the code of onResume to run there instead of here
327  // because the activity has not yet been initialized at this point. At this
328  // point, calls to the keyboard fail.
329  resumedFromListFlag = true;
330  }
331  }
332 
333  // Deleteable implementation
334 
335  @Override
336  public void onDelete() {
337  container.$form().unregisterForActivityResult(this);
338  }
339 
340 }
com.google.appinventor.components.runtime.util.YailList
Definition: YailList.java:26
com.google.appinventor.components.runtime.ListPicker.onResume
void onResume()
Definition: ListPicker.java:105
com.google.appinventor.components.runtime.ListPicker.Elements
void Elements(YailList itemList)
Definition: ListPicker.java:237
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.ListPicker.ListPicker
ListPicker(ComponentContainer container)
Definition: ListPicker.java:93
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.Form.registerForOnResume
void registerForOnResume(OnResumeListener component)
Definition: Form.java:740
com.google.appinventor.components.runtime.ListPicker.ItemBackgroundColor
void ItemBackgroundColor(int argb)
Definition: ListPicker.java:178
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.Component.DEFAULT_VALUE_COLOR_WHITE
static final String DEFAULT_VALUE_COLOR_WHITE
Definition: Component.java:82
com.google.appinventor.components.runtime.util.ElementsUtil.selectionIndex
static int selectionIndex(int index, YailList items)
Definition: ElementsUtil.java:45
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.runtime.ListPicker.ShowFilterBar
void ShowFilterBar(boolean showFilter)
Definition: ListPicker.java:147
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.ListPicker
Definition: ListPicker.java:61
com.google.appinventor.components.runtime.ListPicker.Selection
void Selection(String value)
Definition: ListPicker.java:137
com.google.appinventor.components.runtime.ListPickerActivity
Definition: ListPickerActivity.java:38
com.google.appinventor.components.runtime.util.ElementsUtil.setSelectedIndexFromValue
static int setSelectedIndexFromValue(String value, YailList items)
Definition: ElementsUtil.java:60
com.google.appinventor.components.runtime.OnResumeListener
Definition: OnResumeListener.java:14
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_TEXTAREA
static final String PROPERTY_TYPE_TEXTAREA
Definition: PropertyTypeConstants.java:248
com.google.appinventor.components.runtime.ListPicker.Title
void Title(String title)
Definition: ListPicker.java:281
com.google.appinventor.components.runtime.util.ElementsUtil.elements
static YailList elements(YailList itemList, String componentName)
Definition: ElementsUtil.java:33
com.google.appinventor.components.runtime.ListPicker.ItemBackgroundColor
int ItemBackgroundColor()
Definition: ListPicker.java:188
com.google.appinventor.components.annotations.androidmanifest
Definition: ActionElement.java:6
com.google.appinventor.components.runtime.util.YailList.toStringArray
String[] toStringArray()
Definition: YailList.java:114
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.runtime.ListPicker.resultReturned
void resultReturned(int requestCode, int resultCode, Intent data)
Definition: ListPicker.java:317
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.Component.COLOR_BLACK
static final int COLOR_BLACK
Definition: Component.java:55
com.google.appinventor.components.runtime.ListPicker.onDelete
void onDelete()
Definition: ListPicker.java:336
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.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.runtime.Deleteable
Definition: Deleteable.java:15
com.google.appinventor.components.runtime.util.ElementsUtil.setSelectionFromIndex
static String setSelectionFromIndex(int index, YailList items)
Definition: ElementsUtil.java:53
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.ActivityResultListener
Definition: ActivityResultListener.java:16
com.google.appinventor.components.runtime.Picker
Definition: Picker.java:20
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_COLOR
static final String PROPERTY_TYPE_COLOR
Definition: PropertyTypeConstants.java:63
com.google.appinventor.components.runtime.Component.COLOR_WHITE
static final int COLOR_WHITE
Definition: Component.java:66
com.google.appinventor.components.runtime.ListPicker.getIntent
Intent getIntent()
Definition: ListPicker.java:286
com.google
com
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.annotations.androidmanifest.ActivityElement
Definition: ActivityElement.java:33
com.google.appinventor.components.runtime.ListPicker.ItemTextColor
int ItemTextColor()
Definition: ListPicker.java:171
com.google.appinventor.components.runtime.ListPicker.ItemTextColor
void ItemTextColor(int argb)
Definition: ListPicker.java:164
com.google.appinventor.components.annotations.IsColor
Definition: IsColor.java:13
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.annotations
com.google.appinventor.components.runtime.ListPicker.SelectionIndex
void SelectionIndex(int index)
Definition: ListPicker.java:212
com.google.appinventor.components.runtime.Component.DEFAULT_VALUE_COLOR_BLACK
static final String DEFAULT_VALUE_COLOR_BLACK
Definition: Component.java:71
com.google.appinventor.components.annotations.UsesActivities
Definition: UsesActivities.java:24
com.google.appinventor.components.runtime.util.ElementsUtil
Definition: ElementsUtil.java:15
com.google.appinventor