7 package com.google.appinventor.components.runtime;
9 import android.graphics.Color;
10 import android.graphics.drawable.ColorDrawable;
11 import android.graphics.drawable.Drawable;
12 import android.graphics.drawable.GradientDrawable;
13 import android.graphics.drawable.StateListDrawable;
14 import android.text.Editable;
15 import android.text.Spannable;
16 import android.text.SpannableString;
17 import android.text.TextWatcher;
18 import android.text.style.AbsoluteSizeSpan;
19 import android.text.style.ForegroundColorSpan;
20 import android.view.View;
21 import android.view.ViewGroup;
22 import android.widget.AbsListView;
23 import android.widget.AdapterView;
24 import android.widget.ArrayAdapter;
25 import android.widget.EditText;
26 import android.widget.LinearLayout;
28 import android.widget.LinearLayout.LayoutParams;
56 @DesignerComponent(version = YaVersion.LISTVIEW_COMPONENT_VERSION,
57 description =
"<p>This is a visible component that displays a list of text elements." +
58 " <br> The list can be set using the ElementsFromString property" +
59 " or using the Elements block in the blocks editor. </p>",
60 category = ComponentCategory.USERINTERFACE,
62 iconName =
"images/listView.png")
65 AdapterView.OnItemSelectedListener {
67 private static final String LOG_TAG =
"ListView";
69 private final android.widget.ListView view;
70 private EditText txtSearchBox;
76 private ArrayAdapter<Spannable> adapter;
77 private ArrayAdapter<Spannable> adapterCopy;
79 private int selectionIndex;
80 private String selection;
81 private boolean showFilter =
false;
82 private static final boolean DEFAULT_ENABLED =
false;
84 private int backgroundColor;
88 private int textColor;
91 private int selectionColor;
93 private static final Drawable UNSELECTED_DRAWABLE =
new ColorDrawable(Color.TRANSPARENT);
95 private Drawable selectionDrawable;
96 private View lastSelected;
99 private static final int DEFAULT_TEXT_SIZE = 22;
112 view.setOnItemClickListener(
this);
113 view.setOnItemSelectedListener(
this);
114 view.setChoiceMode(android.widget.ListView.CHOICE_MODE_SINGLE);
115 view.setScrollingCacheEnabled(
false);
116 view.setSelector(
new StateListDrawable());
121 txtSearchBox.setSingleLine(
true);
123 txtSearchBox.setPadding(10, 10, 10, 10);
124 txtSearchBox.setHint(
"Search list...");
126 txtSearchBox.setBackgroundColor(Color.WHITE);
130 txtSearchBox.addTextChangedListener(
new TextWatcher() {
133 public void onTextChanged(CharSequence cs,
int arg1,
int arg2,
int arg3) {
135 adapter.getFilter().filter(cs);
139 public void beforeTextChanged(CharSequence arg0,
int arg1,
int arg2,
int arg3) {
144 public void afterTextChanged(Editable arg0) {
150 txtSearchBox.setVisibility(View.VISIBLE);
152 txtSearchBox.setVisibility(View.GONE);
164 textColor = DEFAULT_TEXT_COLOR;
166 textSize = DEFAULT_TEXT_SIZE;
170 listViewLayout.addView(txtSearchBox);
171 listViewLayout.addView(view);
172 listViewLayout.requestLayout();
178 return listViewLayout;
186 @
SimpleProperty(description =
"Determines the height of the list on the view.",
192 super.Height(height);
200 @
SimpleProperty(description =
"Determines the width of the list on the view.",
216 defaultValue = DEFAULT_ENABLED ?
"True" :
"False")
217 @
SimpleProperty(description =
"Sets visibility of ShowFilterBar. True will show the bar, " +
218 "False will hide it.")
220 this.showFilter = showFilter;
222 txtSearchBox.setVisibility(View.VISIBLE);
225 txtSearchBox.setVisibility(View.GONE);
236 description =
"Returns current state of ShowFilterBar for visibility.")
245 @
SimpleProperty(description=
"List of text elements to show in the ListView. This will " +
246 "signal an error if the elements are not text strings.",
269 @
SimpleProperty(description=
"The TextView elements specified as a string with the " +
270 "items separated by commas " +
271 "such as: Cheese,Fruit,Bacon,Radish. Each word before the comma will be an element in the " +
282 adapter =
new ArrayAdapter<Spannable>(
container.
$context(), android.R.layout.simple_list_item_1,
284 view.setAdapter(adapter);
286 adapterCopy =
new ArrayAdapter<Spannable>(
container.
$context(), android.R.layout.simple_list_item_1);
287 for (
int i = 0; i < adapter.getCount(); ++i) {
288 adapterCopy.insert(adapter.getItem(i), i);
295 int size = items.
size();
296 int displayTextSize = textSize;
297 Spannable [] objects =
new Spannable[size];
298 for (
int i = 1; i <= size; i++) {
306 Spannable chars =
new SpannableString(itemString);
307 chars.setSpan(
new ForegroundColorSpan(textColor),0,chars.length(),0);
309 displayTextSize = (int) (textSize *
container.
$form().deviceDensity());
311 chars.setSpan(
new AbsoluteSizeSpan(displayTextSize),0,chars.length(),0);
312 objects[i - 1] = chars;
324 description =
"The index of the currently selected item, starting at " +
325 "1. If no item is selected, the value will be 0. If an attempt is " +
326 "made to set this to a number less than 1 or greater than the number " +
327 "of items in the ListView, SelectionIndex will be set to 0, and " +
328 "Selection will be set to the empty text.",
331 return selectionIndex;
340 @
SimpleProperty(description=
"Specifies the position of the selected item in the ListView. " +
341 "This could be used to retrieve" +
342 "the text at the chosen position. If an attempt is made to set this to a " +
343 "number less than 1 or greater than the number of items in the ListView, SelectionIndex " +
344 "will be set to 0, and Selection will be set to the empty text."
352 updateSelectionIndex();
358 @
SimpleProperty(description=
"Returns the text last selected in the ListView.",
378 updateSelectionIndex();
385 private void updateSelectionIndex() {
386 if (selectionIndex > 0) {
392 view.requestFocusFromTouch();
396 view.setSelection(selectionIndex - 1);
399 if (previousView !=
null) {
400 previousView.requestFocus();
402 }
else if (lastSelected !=
null) {
404 lastSelected.setBackgroundDrawable(UNSELECTED_DRAWABLE);
414 public void onItemClick(AdapterView<?> parent, View view,
int position,
long id) {
415 Spannable item = (Spannable) parent.getAdapter().getItem(position);
416 this.selection = item.toString();
417 this.selectionIndex = adapterCopy.getPosition(item) + 1;
420 if (lastSelected !=
null) {
421 lastSelected.setBackgroundDrawable(UNSELECTED_DRAWABLE);
425 view.setBackgroundDrawable(selectionDrawable);
438 public void onItemSelected(AdapterView<?> adapterView, View view,
int i,
long l) {
452 @
SimpleEvent(description =
"Simple event to be raised after the an element has been chosen in the" +
453 " list. The selected element is available in the Selection property.")
464 backgroundColor = color;
466 listViewLayout.setBackgroundColor(backgroundColor);
468 view.setCacheColorHint(backgroundColor);
480 description =
"The color of the listview background.",
484 return backgroundColor;
502 backgroundColor = argb;
515 @
SimpleProperty(description =
"The color of the item when it is selected.")
518 return selectionColor;
537 selectionColor = argb;
538 this.selectionDrawable =
new GradientDrawable(
539 GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{argb, argb});
551 description =
"The text color of the listview items.",
583 description =
"The text size of the listview items.",
595 defaultValue = DEFAULT_TEXT_SIZE +
"")