AI2 Component  (Version nb184)
ListPickerActivity.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.content.pm.ActivityInfo;
10 import android.graphics.Color;
11 import android.view.KeyEvent;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.view.ViewGroup;
15 import android.view.WindowManager;
16 import android.widget.AdapterView;
17 import android.widget.ArrayAdapter;
18 import android.widget.EditText;
19 import android.widget.LinearLayout;
20 import android.widget.ListView;
21 import android.widget.TextView;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.text.Editable;
27 import android.text.TextWatcher;
28 import android.view.inputmethod.InputMethodManager;
29 
30 
38 public class ListPickerActivity extends AppInventorCompatActivity implements AdapterView.OnItemClickListener {
39 
40  private String closeAnim = "";
41  private ListView listView;
42 
43  // Listview Adapter
44  MyAdapter adapter;
45 
46  // Search EditText
47  EditText txtSearchBox;
48 
49  static int itemColor;
50  static int backgroundColor;
51 
52  @Override
53  public void onCreate(Bundle savedInstanceState) {
54  super.onCreate(savedInstanceState);
55 
56  styleTitleBar();
57 
58  LinearLayout viewLayout = new LinearLayout(this);
59  viewLayout.setOrientation(LinearLayout.VERTICAL);
60 
61  Intent myIntent = getIntent();
62  if (myIntent.hasExtra(ListPicker.LIST_ACTIVITY_ANIM_TYPE)) {
63  closeAnim = myIntent.getStringExtra(ListPicker.LIST_ACTIVITY_ANIM_TYPE);
64  }
65  if (myIntent.hasExtra(ListPicker.LIST_ACTIVITY_ORIENTATION_TYPE)) {
66  String orientation = myIntent.getStringExtra(ListPicker.LIST_ACTIVITY_ORIENTATION_TYPE).toLowerCase();
67  if (orientation.equals("portrait")) {
68  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
69  }
70  else if (orientation.equals("landscape")) {
71  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
72  }
73  }
74 
75  if (myIntent.hasExtra(ListPicker.LIST_ACTIVITY_TITLE)) {
76  String title = myIntent.getStringExtra(ListPicker.LIST_ACTIVITY_TITLE);
77  setTitle(title);
78  }
79  if (myIntent.hasExtra(ListPicker.LIST_ACTIVITY_ARG_NAME)) {
80  String items[] = getIntent().getStringArrayExtra(ListPicker.LIST_ACTIVITY_ARG_NAME);
81  listView = new ListView(this);
82  listView.setOnItemClickListener(this);
83  listView.setScrollingCacheEnabled(false);
84 
85  itemColor = myIntent.getIntExtra(ListPicker.LIST_ACTIVITY_ITEM_TEXT_COLOR, ListPicker.DEFAULT_ITEM_TEXT_COLOR);
86  backgroundColor = myIntent.getIntExtra(ListPicker.LIST_ACTIVITY_BACKGROUND_COLOR, ListPicker.DEFAULT_ITEM_BACKGROUND_COLOR);
87 
88  viewLayout.setBackgroundColor(backgroundColor);
89 
90  // Adding items to listview
91  adapter = new MyAdapter(this, items);
92  listView.setAdapter(adapter);
93  String showFilterBar =myIntent.getStringExtra(ListPicker.LIST_ACTIVITY_SHOW_SEARCH_BAR);
94 
95  // Determine if we should even show the search bar
96  txtSearchBox = new EditText(this);
97  txtSearchBox.setSingleLine(true);
98  txtSearchBox.setWidth(Component.LENGTH_FILL_PARENT);
99  txtSearchBox.setPadding(10, 10, 10, 10);
100  txtSearchBox.setHint("Search list...");
101  if (!isClassicMode()) {
102  txtSearchBox.setBackgroundColor(Color.WHITE);
103  }
104 
105  if (showFilterBar == null || !showFilterBar.equalsIgnoreCase("true")) {
106  txtSearchBox.setVisibility(View.GONE);
107  }
108 
109  //set up the listener
110  txtSearchBox.addTextChangedListener(new TextWatcher() {
111 
112  @Override
113  public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
114  // When user changed the Text
115  ListPickerActivity.this.adapter.getFilter().filter(cs);
116  }
117 
118  @Override
119  public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
120  // no-op. Required method
121  }
122 
123  @Override
124  public void afterTextChanged(Editable arg0) {
125  // no-op. Required method
126  }
127  });
128 
129  }
130  else {
131  setResult(RESULT_CANCELED);
132  finish();
133  AnimationUtil.ApplyCloseScreenAnimation(this, closeAnim);
134  }
135  viewLayout.addView(txtSearchBox);
136  viewLayout.addView(listView);
137 
138  this.setContentView(viewLayout);
139  viewLayout.requestLayout();
140 
141  //hide the keyboard
142  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
143  imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
144  getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
145  }
146 
147  @Override
148  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
149  String selected = (String) parent.getAdapter().getItem(position);
150  Intent resultIntent = new Intent();
151  resultIntent.putExtra(ListPicker.LIST_ACTIVITY_RESULT_NAME, selected);
152  resultIntent.putExtra(ListPicker.LIST_ACTIVITY_RESULT_INDEX, position + 1);
153  closeAnim = selected;
154  setResult(RESULT_OK, resultIntent);
155  finish();
156  AnimationUtil.ApplyCloseScreenAnimation(this, closeAnim);
157  }
158 
159  // Capture the hardware back button to make sure the screen animation
160  // still applies.
161  @Override
162  public void onBackPressed() {
163  AnimationUtil.ApplyCloseScreenAnimation(this, closeAnim);
164  super.onBackPressed();
165  }
166 
167 
168  private static class MyAdapter extends ArrayAdapter<String> {
169 
170  private final Context mContext;
171 
172  public MyAdapter(final Context context, final String[] items) {
173  super(context, android.R.layout.activity_list_item, items);
174  mContext = context;
175  }
176 
177  @Override
178  public long getItemId(final int position) {
179  return getItem(position).hashCode();
180  }
181 
182  @Override
183  public View getView(final int position, final View convertView, final ViewGroup parent) {
184  TextView tv = (TextView) convertView;
185  if (tv == null) {
186  tv = (TextView) LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_1, parent, false);
187  }
188  tv.setText(getItem(position));
189  tv.setTextColor(itemColor);
190  return tv;
191  }
192  }
193 }
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.ListPicker.DEFAULT_ITEM_BACKGROUND_COLOR
static final int DEFAULT_ITEM_BACKGROUND_COLOR
Definition: ListPicker.java:86
com.google.appinventor.components
com.google.appinventor.components.runtime.Component.LENGTH_FILL_PARENT
static final int LENGTH_FILL_PARENT
Definition: Component.java:119
com.google.appinventor.components.runtime.util.AnimationUtil
Definition: AnimationUtil.java:24
com.google.appinventor.components.runtime.ListPickerActivity.onItemClick
void onItemClick(AdapterView<?> parent, View view, int position, long id)
Definition: ListPickerActivity.java:148
com.google.appinventor.components.runtime.ListPicker
Definition: ListPicker.java:61
com.google.appinventor.components.runtime.AppInventorCompatActivity.styleTitleBar
void styleTitleBar()
Definition: AppInventorCompatActivity.java:292
com.google.appinventor.components.runtime.ListPickerActivity
Definition: ListPickerActivity.java:38
com.google.appinventor.components.runtime.ListPickerActivity.onCreate
void onCreate(Bundle savedInstanceState)
Definition: ListPickerActivity.java:53
com.google.appinventor.components.runtime.ListPickerActivity.onBackPressed
void onBackPressed()
Definition: ListPickerActivity.java:162
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.ListView
Definition: ListView.java:64
com.google.appinventor.components.runtime.AppInventorCompatActivity.setContentView
void setContentView(View view)
Definition: AppInventorCompatActivity.java:190
com.google.appinventor.components.runtime.AppInventorCompatActivity
Definition: AppInventorCompatActivity.java:40
com.google
com
com.google.appinventor.components.runtime.AppInventorCompatActivity.isClassicMode
static boolean isClassicMode()
Definition: AppInventorCompatActivity.java:237
com.google.appinventor.components.runtime.LinearLayout
Definition: LinearLayout.java:20
com.google.appinventor.components.runtime.ListPicker.DEFAULT_ITEM_TEXT_COLOR
static final int DEFAULT_ITEM_TEXT_COLOR
Definition: ListPicker.java:85
com.google.appinventor
com.google.appinventor.components.runtime.util.AnimationUtil.ApplyCloseScreenAnimation
static void ApplyCloseScreenAnimation(Activity activity, String animType)
Definition: AnimationUtil.java:121