7 package com.google.appinventor.components.runtime;
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;
40 private String closeAnim =
"";
47 EditText txtSearchBox;
50 static int backgroundColor;
53 public void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
61 Intent myIntent = getIntent();
62 if (myIntent.hasExtra(
ListPicker.LIST_ACTIVITY_ANIM_TYPE)) {
63 closeAnim = myIntent.getStringExtra(
ListPicker.LIST_ACTIVITY_ANIM_TYPE);
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);
70 else if (orientation.equals(
"landscape")) {
71 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
75 if (myIntent.hasExtra(
ListPicker.LIST_ACTIVITY_TITLE)) {
76 String title = myIntent.getStringExtra(
ListPicker.LIST_ACTIVITY_TITLE);
79 if (myIntent.hasExtra(
ListPicker.LIST_ACTIVITY_ARG_NAME)) {
80 String items[] = getIntent().getStringArrayExtra(
ListPicker.LIST_ACTIVITY_ARG_NAME);
82 listView.setOnItemClickListener(
this);
83 listView.setScrollingCacheEnabled(
false);
88 viewLayout.setBackgroundColor(backgroundColor);
91 adapter =
new MyAdapter(
this, items);
92 listView.setAdapter(adapter);
93 String showFilterBar =myIntent.getStringExtra(
ListPicker.LIST_ACTIVITY_SHOW_SEARCH_BAR);
96 txtSearchBox =
new EditText(
this);
97 txtSearchBox.setSingleLine(
true);
99 txtSearchBox.setPadding(10, 10, 10, 10);
100 txtSearchBox.setHint(
"Search list...");
102 txtSearchBox.setBackgroundColor(Color.WHITE);
105 if (showFilterBar ==
null || !showFilterBar.equalsIgnoreCase(
"true")) {
106 txtSearchBox.setVisibility(View.GONE);
110 txtSearchBox.addTextChangedListener(
new TextWatcher() {
113 public void onTextChanged(CharSequence cs,
int arg1,
int arg2,
int arg3) {
119 public void beforeTextChanged(CharSequence arg0,
int arg1,
int arg2,
int arg3) {
124 public void afterTextChanged(Editable arg0) {
131 setResult(RESULT_CANCELED);
135 viewLayout.addView(txtSearchBox);
136 viewLayout.addView(listView);
139 viewLayout.requestLayout();
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);
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);
164 super.onBackPressed();
168 private static class MyAdapter
extends ArrayAdapter<String> {
170 private final Context mContext;
172 public MyAdapter(
final Context context,
final String[] items) {
173 super(context, android.R.layout.activity_list_item, items);
178 public long getItemId(
final int position) {
179 return getItem(position).hashCode();
183 public View getView(
final int position,
final View convertView,
final ViewGroup parent) {
184 TextView tv = (TextView) convertView;
186 tv = (TextView) LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_1, parent,
false);
188 tv.setText(getItem(position));
189 tv.setTextColor(itemColor);