blob: eb232fd8db5c665bd8fbc433590686c448ae5bb7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import android.content.Context;
20import android.content.res.TypedArray;
Romain Guy6a678102010-03-16 13:49:31 -070021import android.database.DataSetObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.graphics.Rect;
23import android.graphics.drawable.Drawable;
24import android.text.Editable;
25import android.text.Selection;
26import android.text.TextUtils;
27import android.text.TextWatcher;
28import android.util.AttributeSet;
29import android.util.Log;
30import android.view.KeyEvent;
31import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.view.View;
33import android.view.ViewGroup;
Romain Guy374aaaed32009-07-14 15:11:59 -070034import android.view.WindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.view.inputmethod.CompletionInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.view.inputmethod.EditorInfo;
Adam Powellc3fa6302010-05-18 11:36:27 -070037import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Gilles Debunne688df792011-09-14 15:52:03 -070039import com.android.internal.R;
40
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42/**
43 * <p>An editable text view that shows completion suggestions automatically
44 * while the user is typing. The list of suggestions is displayed in a drop
45 * down menu from which the user can choose an item to replace the content
46 * of the edit box with.</p>
47 *
48 * <p>The drop down can be dismissed at any time by pressing the back key or,
49 * if no item is selected in the drop down, by pressing the enter/dpad center
50 * key.</p>
51 *
52 * <p>The list of suggestions is obtained from a data adapter and appears
53 * only after a given number of characters defined by
54 * {@link #getThreshold() the threshold}.</p>
55 *
56 * <p>The following code snippet shows how to create a text view which suggests
57 * various countries names while the user is typing:</p>
58 *
59 * <pre class="prettyprint">
60 * public class CountriesActivity extends Activity {
61 * protected void onCreate(Bundle icicle) {
62 * super.onCreate(icicle);
63 * setContentView(R.layout.countries);
64 *
65 * ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,
66 * android.R.layout.simple_dropdown_item_1line, COUNTRIES);
67 * AutoCompleteTextView textView = (AutoCompleteTextView)
68 * findViewById(R.id.countries_list);
69 * textView.setAdapter(adapter);
70 * }
71 *
72 * private static final String[] COUNTRIES = new String[] {
73 * "Belgium", "France", "Italy", "Germany", "Spain"
74 * };
75 * }
76 * </pre>
77 *
Scott Main4c359b72012-07-24 15:51:27 -070078 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/text.html">Text Fields</a>
79 * guide.</p>
Scott Main41ec6532010-08-19 16:57:07 -070080 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 * @attr ref android.R.styleable#AutoCompleteTextView_completionHint
82 * @attr ref android.R.styleable#AutoCompleteTextView_completionThreshold
83 * @attr ref android.R.styleable#AutoCompleteTextView_completionHintView
84 * @attr ref android.R.styleable#AutoCompleteTextView_dropDownSelector
85 * @attr ref android.R.styleable#AutoCompleteTextView_dropDownAnchor
86 * @attr ref android.R.styleable#AutoCompleteTextView_dropDownWidth
Romain Guye29f0642009-06-23 21:27:02 -070087 * @attr ref android.R.styleable#AutoCompleteTextView_dropDownHeight
Romain Guyaa1c6312009-09-30 11:36:01 -070088 * @attr ref android.R.styleable#AutoCompleteTextView_dropDownVerticalOffset
89 * @attr ref android.R.styleable#AutoCompleteTextView_dropDownHorizontalOffset
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 */
91public class AutoCompleteTextView extends EditText implements Filter.FilterListener {
92 static final boolean DEBUG = false;
93 static final String TAG = "AutoCompleteTextView";
94
Adam Powell348e69c2011-02-16 16:49:50 -080095 static final int EXPAND_MAX = 3;
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private CharSequence mHintText;
Adam Powellc3fa6302010-05-18 11:36:27 -070098 private TextView mHintView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 private int mHintResource;
100
101 private ListAdapter mAdapter;
102 private Filter mFilter;
103 private int mThreshold;
104
Adam Powellc3fa6302010-05-18 11:36:27 -0700105 private ListPopupWindow mPopup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 private int mDropDownAnchorId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 private AdapterView.OnItemClickListener mItemClickListener;
109 private AdapterView.OnItemSelectedListener mItemSelectedListener;
110
Karl Rosaen875d50a2009-04-23 19:00:21 -0700111 private boolean mDropDownDismissedOnCompletion = true;
112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 private int mLastKeyCode = KeyEvent.KEYCODE_UNKNOWN;
114 private boolean mOpenBefore;
115
116 private Validator mValidator = null;
117
Gilles Debunnebe2c4f92011-01-17 15:14:32 -0800118 // Set to true when text is set directly and no filtering shall be performed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 private boolean mBlockCompletion;
120
Gilles Debunne4a74dbc2011-01-23 17:23:31 -0800121 // When set, an update in the underlying adapter will update the result list popup.
122 // Set to false when the list is hidden to prevent asynchronous updates to popup the list again.
123 private boolean mPopupCanBeUpdated = true;
124
Romain Guy6a678102010-03-16 13:49:31 -0700125 private PassThroughClickListener mPassThroughClickListener;
126 private PopupDataSetObserver mObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
128 public AutoCompleteTextView(Context context) {
129 this(context, null);
130 }
131
132 public AutoCompleteTextView(Context context, AttributeSet attrs) {
133 this(context, attrs, com.android.internal.R.attr.autoCompleteTextViewStyle);
134 }
135
Alan Viverette617feb92013-09-09 18:09:13 -0700136 public AutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
137 this(context, attrs, defStyleAttr, 0);
138 }
139
140 public AutoCompleteTextView(
141 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
142 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Adam Powellc3fa6302010-05-18 11:36:27 -0700144 mPopup = new ListPopupWindow(context, attrs,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 com.android.internal.R.attr.autoCompleteTextViewStyle);
Romain Guy374aaaed32009-07-14 15:11:59 -0700146 mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Adam Powellc3fa6302010-05-18 11:36:27 -0700147 mPopup.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
Alan Viverette617feb92013-09-09 18:09:13 -0700149 final TypedArray a = context.obtainStyledAttributes(attrs,
150 com.android.internal.R.styleable.AutoCompleteTextView, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151
152 mThreshold = a.getInt(
153 R.styleable.AutoCompleteTextView_completionThreshold, 2);
154
Adam Powellc3fa6302010-05-18 11:36:27 -0700155 mPopup.setListSelector(a.getDrawable(R.styleable.AutoCompleteTextView_dropDownSelector));
156 mPopup.setVerticalOffset((int)
157 a.getDimension(R.styleable.AutoCompleteTextView_dropDownVerticalOffset, 0.0f));
158 mPopup.setHorizontalOffset((int)
159 a.getDimension(R.styleable.AutoCompleteTextView_dropDownHorizontalOffset, 0.0f));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
161 // Get the anchor's id now, but the view won't be ready, so wait to actually get the
162 // view and store it in mDropDownAnchorView lazily in getDropDownAnchorView later.
163 // Defaults to NO_ID, in which case the getDropDownAnchorView method will simply return
164 // this TextView, as a default anchoring point.
165 mDropDownAnchorId = a.getResourceId(R.styleable.AutoCompleteTextView_dropDownAnchor,
166 View.NO_ID);
167
Romain Guy980a9382010-01-08 15:06:28 -0800168 // For dropdown width, the developer can specify a specific width, or MATCH_PARENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 // (for full screen width) or WRAP_CONTENT (to match the width of the anchored view).
Adam Powellc3fa6302010-05-18 11:36:27 -0700170 mPopup.setWidth(a.getLayoutDimension(
171 R.styleable.AutoCompleteTextView_dropDownWidth,
172 ViewGroup.LayoutParams.WRAP_CONTENT));
173 mPopup.setHeight(a.getLayoutDimension(
174 R.styleable.AutoCompleteTextView_dropDownHeight,
175 ViewGroup.LayoutParams.WRAP_CONTENT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
177 mHintResource = a.getResourceId(R.styleable.AutoCompleteTextView_completionHintView,
178 R.layout.simple_dropdown_hint);
Adam Powellc3fa6302010-05-18 11:36:27 -0700179
180 mPopup.setOnItemClickListener(new DropDownItemClickListener());
181 setCompletionHint(a.getText(R.styleable.AutoCompleteTextView_completionHint));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
183 // Always turn on the auto complete input type flag, since it
184 // makes no sense to use this widget without it.
185 int inputType = getInputType();
186 if ((inputType&EditorInfo.TYPE_MASK_CLASS)
187 == EditorInfo.TYPE_CLASS_TEXT) {
188 inputType |= EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE;
189 setRawInputType(inputType);
190 }
191
192 a.recycle();
193
194 setFocusable(true);
195
196 addTextChangedListener(new MyWatcher());
Mike LeBeauddf98562009-05-08 17:59:05 -0700197
Karl Rosaen98e333f2009-04-28 10:39:09 -0700198 mPassThroughClickListener = new PassThroughClickListener();
199 super.setOnClickListener(mPassThroughClickListener);
200 }
201
202 @Override
203 public void setOnClickListener(OnClickListener listener) {
204 mPassThroughClickListener.mWrapped = listener;
205 }
206
207 /**
208 * Private hook into the on click event, dispatched from {@link PassThroughClickListener}
209 */
210 private void onClickImpl() {
Amith Yamasanid25eb352010-03-10 21:09:28 -0800211 // If the dropdown is showing, bring the keyboard to the front
212 // when the user touches the text field.
Gilles Debunne711734a2011-02-07 18:26:11 -0800213 if (isPopupShowing()) {
Amith Yamasanid25eb352010-03-10 21:09:28 -0800214 ensureImeVisible(true);
Karl Rosaen98e333f2009-04-28 10:39:09 -0700215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 }
217
218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * <p>Sets the optional hint text that is displayed at the bottom of the
220 * the matching list. This can be used as a cue to the user on how to
221 * best use the list, or to provide extra information.</p>
222 *
223 * @param hint the text to be displayed to the user
224 *
Gilles Debunne5c49d112012-04-30 16:44:14 -0700225 * @see #getCompletionHint()
226 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 * @attr ref android.R.styleable#AutoCompleteTextView_completionHint
228 */
229 public void setCompletionHint(CharSequence hint) {
230 mHintText = hint;
Adam Powellc3fa6302010-05-18 11:36:27 -0700231 if (hint != null) {
232 if (mHintView == null) {
233 final TextView hintView = (TextView) LayoutInflater.from(getContext()).inflate(
234 mHintResource, null).findViewById(com.android.internal.R.id.text1);
235 hintView.setText(mHintText);
236 mHintView = hintView;
237 mPopup.setPromptView(hintView);
238 } else {
239 mHintView.setText(hint);
240 }
241 } else {
242 mPopup.setPromptView(null);
243 mHintView = null;
244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
Gilles Debunne5c49d112012-04-30 16:44:14 -0700246
247 /**
248 * Gets the optional hint text displayed at the bottom of the the matching list.
249 *
250 * @return The hint text, if any
251 *
252 * @see #setCompletionHint(CharSequence)
253 *
254 * @attr ref android.R.styleable#AutoCompleteTextView_completionHint
255 */
256 public CharSequence getCompletionHint() {
257 return mHintText;
258 }
259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * <p>Returns the current width for the auto-complete drop down list. This can
Romain Guy980a9382010-01-08 15:06:28 -0800262 * be a fixed width, or {@link ViewGroup.LayoutParams#MATCH_PARENT} to fill the screen, or
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 * {@link ViewGroup.LayoutParams#WRAP_CONTENT} to fit the width of its anchor view.</p>
264 *
265 * @return the width for the drop down list
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700266 *
267 * @attr ref android.R.styleable#AutoCompleteTextView_dropDownWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 */
269 public int getDropDownWidth() {
Adam Powellc3fa6302010-05-18 11:36:27 -0700270 return mPopup.getWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 }
272
273 /**
274 * <p>Sets the current width for the auto-complete drop down list. This can
Romain Guy980a9382010-01-08 15:06:28 -0800275 * be a fixed width, or {@link ViewGroup.LayoutParams#MATCH_PARENT} to fill the screen, or
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 * {@link ViewGroup.LayoutParams#WRAP_CONTENT} to fit the width of its anchor view.</p>
277 *
278 * @param width the width to use
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700279 *
280 * @attr ref android.R.styleable#AutoCompleteTextView_dropDownWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 */
282 public void setDropDownWidth(int width) {
Adam Powellc3fa6302010-05-18 11:36:27 -0700283 mPopup.setWidth(width);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 }
Romain Guye29f0642009-06-23 21:27:02 -0700285
286 /**
287 * <p>Returns the current height for the auto-complete drop down list. This can
Romain Guy980a9382010-01-08 15:06:28 -0800288 * be a fixed height, or {@link ViewGroup.LayoutParams#MATCH_PARENT} to fill
Romain Guye29f0642009-06-23 21:27:02 -0700289 * the screen, or {@link ViewGroup.LayoutParams#WRAP_CONTENT} to fit the height
290 * of the drop down's content.</p>
291 *
292 * @return the height for the drop down list
293 *
294 * @attr ref android.R.styleable#AutoCompleteTextView_dropDownHeight
295 */
296 public int getDropDownHeight() {
Adam Powellc3fa6302010-05-18 11:36:27 -0700297 return mPopup.getHeight();
Romain Guye29f0642009-06-23 21:27:02 -0700298 }
299
300 /**
301 * <p>Sets the current height for the auto-complete drop down list. This can
Romain Guy980a9382010-01-08 15:06:28 -0800302 * be a fixed height, or {@link ViewGroup.LayoutParams#MATCH_PARENT} to fill
Romain Guye29f0642009-06-23 21:27:02 -0700303 * the screen, or {@link ViewGroup.LayoutParams#WRAP_CONTENT} to fit the height
304 * of the drop down's content.</p>
305 *
306 * @param height the height to use
307 *
308 * @attr ref android.R.styleable#AutoCompleteTextView_dropDownHeight
309 */
310 public void setDropDownHeight(int height) {
Adam Powellc3fa6302010-05-18 11:36:27 -0700311 mPopup.setHeight(height);
Romain Guye29f0642009-06-23 21:27:02 -0700312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313
314 /**
315 * <p>Returns the id for the view that the auto-complete drop down list is anchored to.</p>
316 *
317 * @return the view's id, or {@link View#NO_ID} if none specified
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700318 *
319 * @attr ref android.R.styleable#AutoCompleteTextView_dropDownAnchor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 */
321 public int getDropDownAnchor() {
322 return mDropDownAnchorId;
323 }
324
325 /**
326 * <p>Sets the view to which the auto-complete drop down list should anchor. The view
327 * corresponding to this id will not be loaded until the next time it is needed to avoid
328 * loading a view which is not yet instantiated.</p>
329 *
330 * @param id the id to anchor the drop down list view to
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700331 *
332 * @attr ref android.R.styleable#AutoCompleteTextView_dropDownAnchor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 */
334 public void setDropDownAnchor(int id) {
335 mDropDownAnchorId = id;
Adam Powellc3fa6302010-05-18 11:36:27 -0700336 mPopup.setAnchorView(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 }
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700338
339 /**
340 * <p>Gets the background of the auto-complete drop-down list.</p>
341 *
342 * @return the background drawable
343 *
344 * @attr ref android.R.styleable#PopupWindow_popupBackground
345 */
346 public Drawable getDropDownBackground() {
347 return mPopup.getBackground();
348 }
349
350 /**
351 * <p>Sets the background of the auto-complete drop-down list.</p>
352 *
353 * @param d the drawable to set as the background
354 *
355 * @attr ref android.R.styleable#PopupWindow_popupBackground
356 */
357 public void setDropDownBackgroundDrawable(Drawable d) {
358 mPopup.setBackgroundDrawable(d);
359 }
360
361 /**
362 * <p>Sets the background of the auto-complete drop-down list.</p>
363 *
364 * @param id the id of the drawable to set as the background
365 *
366 * @attr ref android.R.styleable#PopupWindow_popupBackground
367 */
368 public void setDropDownBackgroundResource(int id) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800369 mPopup.setBackgroundDrawable(getContext().getDrawable(id));
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700370 }
371
372 /**
373 * <p>Sets the vertical offset used for the auto-complete drop-down list.</p>
374 *
375 * @param offset the vertical offset
376 */
377 public void setDropDownVerticalOffset(int offset) {
Adam Powellc3fa6302010-05-18 11:36:27 -0700378 mPopup.setVerticalOffset(offset);
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700379 }
380
381 /**
382 * <p>Gets the vertical offset used for the auto-complete drop-down list.</p>
383 *
384 * @return the vertical offset
385 */
386 public int getDropDownVerticalOffset() {
Adam Powellc3fa6302010-05-18 11:36:27 -0700387 return mPopup.getVerticalOffset();
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700388 }
389
390 /**
391 * <p>Sets the horizontal offset used for the auto-complete drop-down list.</p>
392 *
393 * @param offset the horizontal offset
394 */
395 public void setDropDownHorizontalOffset(int offset) {
Adam Powellc3fa6302010-05-18 11:36:27 -0700396 mPopup.setHorizontalOffset(offset);
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700397 }
398
399 /**
400 * <p>Gets the horizontal offset used for the auto-complete drop-down list.</p>
401 *
402 * @return the horizontal offset
403 */
404 public int getDropDownHorizontalOffset() {
Adam Powellc3fa6302010-05-18 11:36:27 -0700405 return mPopup.getHorizontalOffset();
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700406 }
The Android Open Source Project8f080ec2009-04-29 13:34:51 -0700407
The Android Open Source Projectbff13892009-04-29 13:41:02 -0700408 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700409 * <p>Sets the animation style of the auto-complete drop-down list.</p>
410 *
411 * <p>If the drop-down is showing, calling this method will take effect only
412 * the next time the drop-down is shown.</p>
413 *
414 * @param animationStyle animation style to use when the drop-down appears
415 * and disappears. Set to -1 for the default animation, 0 for no
416 * animation, or a resource identifier for an explicit animation.
417 *
418 * @hide Pending API council approval
419 */
420 public void setDropDownAnimationStyle(int animationStyle) {
421 mPopup.setAnimationStyle(animationStyle);
422 }
423
424 /**
425 * <p>Returns the animation style that is used when the drop-down list appears and disappears
426 * </p>
427 *
428 * @return the animation style that is used when the drop-down list appears and disappears
429 *
430 * @hide Pending API council approval
431 */
432 public int getDropDownAnimationStyle() {
433 return mPopup.getAnimationStyle();
434 }
Karl Rosaen875d50a2009-04-23 19:00:21 -0700435
436 /**
437 * @return Whether the drop-down is visible as long as there is {@link #enoughToFilter()}
438 *
439 * @hide Pending API council approval
440 */
441 public boolean isDropDownAlwaysVisible() {
Adam Powellc3fa6302010-05-18 11:36:27 -0700442 return mPopup.isDropDownAlwaysVisible();
Karl Rosaen875d50a2009-04-23 19:00:21 -0700443 }
444
445 /**
446 * Sets whether the drop-down should remain visible as long as there is there is
447 * {@link #enoughToFilter()}. This is useful if an unknown number of results are expected
448 * to show up in the adapter sometime in the future.
449 *
450 * The drop-down will occupy the entire screen below {@link #getDropDownAnchor} regardless
451 * of the size or content of the list. {@link #getDropDownBackground()} will fill any space
452 * that is not used by the list.
453 *
454 * @param dropDownAlwaysVisible Whether to keep the drop-down visible.
455 *
456 * @hide Pending API council approval
457 */
458 public void setDropDownAlwaysVisible(boolean dropDownAlwaysVisible) {
Adam Powellc3fa6302010-05-18 11:36:27 -0700459 mPopup.setDropDownAlwaysVisible(dropDownAlwaysVisible);
Karl Rosaen875d50a2009-04-23 19:00:21 -0700460 }
461
462 /**
463 * Checks whether the drop-down is dismissed when a suggestion is clicked.
464 *
465 * @hide Pending API council approval
466 */
467 public boolean isDropDownDismissedOnCompletion() {
468 return mDropDownDismissedOnCompletion;
469 }
Romain Guy6a678102010-03-16 13:49:31 -0700470
Karl Rosaen875d50a2009-04-23 19:00:21 -0700471 /**
472 * Sets whether the drop-down is dismissed when a suggestion is clicked. This is
473 * true by default.
474 *
475 * @param dropDownDismissedOnCompletion Whether to dismiss the drop-down.
476 *
477 * @hide Pending API council approval
478 */
479 public void setDropDownDismissedOnCompletion(boolean dropDownDismissedOnCompletion) {
480 mDropDownDismissedOnCompletion = dropDownDismissedOnCompletion;
481 }
482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 /**
484 * <p>Returns the number of characters the user must type before the drop
485 * down list is shown.</p>
486 *
487 * @return the minimum number of characters to type to show the drop down
488 *
489 * @see #setThreshold(int)
Gilles Debunne5c49d112012-04-30 16:44:14 -0700490 *
491 * @attr ref android.R.styleable#AutoCompleteTextView_completionThreshold
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 */
493 public int getThreshold() {
494 return mThreshold;
495 }
496
497 /**
498 * <p>Specifies the minimum number of characters the user has to type in the
499 * edit box before the drop down list is shown.</p>
500 *
501 * <p>When <code>threshold</code> is less than or equals 0, a threshold of
502 * 1 is applied.</p>
503 *
504 * @param threshold the number of characters to type before the drop down
505 * is shown
506 *
507 * @see #getThreshold()
508 *
509 * @attr ref android.R.styleable#AutoCompleteTextView_completionThreshold
510 */
511 public void setThreshold(int threshold) {
512 if (threshold <= 0) {
513 threshold = 1;
514 }
515
516 mThreshold = threshold;
517 }
518
519 /**
520 * <p>Sets the listener that will be notified when the user clicks an item
521 * in the drop down list.</p>
522 *
523 * @param l the item click listener
524 */
525 public void setOnItemClickListener(AdapterView.OnItemClickListener l) {
526 mItemClickListener = l;
527 }
528
529 /**
530 * <p>Sets the listener that will be notified when the user selects an item
531 * in the drop down list.</p>
532 *
533 * @param l the item selected listener
534 */
535 public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener l) {
536 mItemSelectedListener = l;
537 }
538
539 /**
540 * <p>Returns the listener that is notified whenever the user clicks an item
541 * in the drop down list.</p>
542 *
543 * @return the item click listener
544 *
545 * @deprecated Use {@link #getOnItemClickListener()} intead
546 */
547 @Deprecated
548 public AdapterView.OnItemClickListener getItemClickListener() {
549 return mItemClickListener;
550 }
551
552 /**
553 * <p>Returns the listener that is notified whenever the user selects an
554 * item in the drop down list.</p>
555 *
556 * @return the item selected listener
557 *
558 * @deprecated Use {@link #getOnItemSelectedListener()} intead
559 */
560 @Deprecated
561 public AdapterView.OnItemSelectedListener getItemSelectedListener() {
562 return mItemSelectedListener;
563 }
564
565 /**
566 * <p>Returns the listener that is notified whenever the user clicks an item
567 * in the drop down list.</p>
568 *
569 * @return the item click listener
570 */
571 public AdapterView.OnItemClickListener getOnItemClickListener() {
572 return mItemClickListener;
573 }
574
575 /**
576 * <p>Returns the listener that is notified whenever the user selects an
577 * item in the drop down list.</p>
578 *
579 * @return the item selected listener
580 */
581 public AdapterView.OnItemSelectedListener getOnItemSelectedListener() {
582 return mItemSelectedListener;
583 }
584
585 /**
Adam Powell780c4912012-08-30 17:30:05 -0700586 * Set a listener that will be invoked whenever the AutoCompleteTextView's
587 * list of completions is dismissed.
588 * @param dismissListener Listener to invoke when completions are dismissed
589 */
590 public void setOnDismissListener(final OnDismissListener dismissListener) {
591 PopupWindow.OnDismissListener wrappedListener = null;
592 if (dismissListener != null) {
593 wrappedListener = new PopupWindow.OnDismissListener() {
594 @Override public void onDismiss() {
595 dismissListener.onDismiss();
596 }
597 };
598 }
599 mPopup.setOnDismissListener(wrappedListener);
600 }
601
602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 * <p>Returns a filterable list adapter used for auto completion.</p>
604 *
605 * @return a data adapter used for auto completion
606 */
607 public ListAdapter getAdapter() {
608 return mAdapter;
609 }
610
611 /**
612 * <p>Changes the list of data used for auto completion. The provided list
613 * must be a filterable list adapter.</p>
614 *
615 * <p>The caller is still responsible for managing any resources used by the adapter.
616 * Notably, when the AutoCompleteTextView is closed or released, the adapter is not notified.
617 * A common case is the use of {@link android.widget.CursorAdapter}, which
618 * contains a {@link android.database.Cursor} that must be closed. This can be done
619 * automatically (see
620 * {@link android.app.Activity#startManagingCursor(android.database.Cursor)
621 * startManagingCursor()}),
622 * or by manually closing the cursor when the AutoCompleteTextView is dismissed.</p>
623 *
624 * @param adapter the adapter holding the auto completion data
625 *
626 * @see #getAdapter()
627 * @see android.widget.Filterable
628 * @see android.widget.ListAdapter
629 */
630 public <T extends ListAdapter & Filterable> void setAdapter(T adapter) {
Romain Guy6a678102010-03-16 13:49:31 -0700631 if (mObserver == null) {
632 mObserver = new PopupDataSetObserver();
633 } else if (mAdapter != null) {
634 mAdapter.unregisterDataSetObserver(mObserver);
635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 mAdapter = adapter;
637 if (mAdapter != null) {
638 //noinspection unchecked
639 mFilter = ((Filterable) mAdapter).getFilter();
Romain Guy6a678102010-03-16 13:49:31 -0700640 adapter.registerDataSetObserver(mObserver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 } else {
642 mFilter = null;
643 }
644
Adam Powellc3fa6302010-05-18 11:36:27 -0700645 mPopup.setAdapter(mAdapter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 }
647
648 @Override
649 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -0700650 if (keyCode == KeyEvent.KEYCODE_BACK && isPopupShowing()
Adam Powellc3fa6302010-05-18 11:36:27 -0700651 && !mPopup.isDropDownAlwaysVisible()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 // special case for the back key, we do not even try to send it
653 // to the drop down list but instead, consume it immediately
Romain Guy2beee4d2010-03-15 17:18:22 -0700654 if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
Jeff Brownb3ea9222011-01-10 16:26:36 -0800655 KeyEvent.DispatcherState state = getKeyDispatcherState();
656 if (state != null) {
657 state.startTracking(event, this);
658 }
Dianne Hackborn8d374262009-09-14 21:21:52 -0700659 return true;
Bjorn Bringert4eb3efc2009-10-06 09:26:06 +0100660 } else if (event.getAction() == KeyEvent.ACTION_UP) {
Jeff Brownb3ea9222011-01-10 16:26:36 -0800661 KeyEvent.DispatcherState state = getKeyDispatcherState();
662 if (state != null) {
663 state.handleUpEvent(event);
664 }
Bjorn Bringert4eb3efc2009-10-06 09:26:06 +0100665 if (event.isTracking() && !event.isCanceled()) {
666 dismissDropDown();
667 return true;
668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 }
670 }
671 return super.onKeyPreIme(keyCode, event);
672 }
673
674 @Override
675 public boolean onKeyUp(int keyCode, KeyEvent event) {
Adam Powellc3fa6302010-05-18 11:36:27 -0700676 boolean consumed = mPopup.onKeyUp(keyCode, event);
677 if (consumed) {
678 switch (keyCode) {
679 // if the list accepts the key events and the key event
680 // was a click, the text view gets the selected item
681 // from the drop down as its content
682 case KeyEvent.KEYCODE_ENTER:
683 case KeyEvent.KEYCODE_DPAD_CENTER:
Jeff Brown4e6319b2010-12-13 10:36:51 -0800684 case KeyEvent.KEYCODE_TAB:
685 if (event.hasNoModifiers()) {
686 performCompletion();
687 }
Adam Powellc3fa6302010-05-18 11:36:27 -0700688 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 }
690 }
Gilles Debunnea4a57582011-02-08 18:21:01 -0800691
692 if (isPopupShowing() && keyCode == KeyEvent.KEYCODE_TAB && event.hasNoModifiers()) {
693 performCompletion();
694 return true;
695 }
696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 return super.onKeyUp(keyCode, event);
698 }
699
700 @Override
701 public boolean onKeyDown(int keyCode, KeyEvent event) {
Adam Powellc3fa6302010-05-18 11:36:27 -0700702 if (mPopup.onKeyDown(keyCode, event)) {
703 return true;
704 }
705
706 if (!isPopupShowing()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 switch(keyCode) {
708 case KeyEvent.KEYCODE_DPAD_DOWN:
Jeff Brown4e6319b2010-12-13 10:36:51 -0800709 if (event.hasNoModifiers()) {
710 performValidation();
711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 }
713 }
714
Gilles Debunnea4a57582011-02-08 18:21:01 -0800715 if (isPopupShowing() && keyCode == KeyEvent.KEYCODE_TAB && event.hasNoModifiers()) {
716 return true;
717 }
718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 mLastKeyCode = keyCode;
720 boolean handled = super.onKeyDown(keyCode, event);
721 mLastKeyCode = KeyEvent.KEYCODE_UNKNOWN;
722
Adam Powellc3fa6302010-05-18 11:36:27 -0700723 if (handled && isPopupShowing()) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700724 clearListSelection();
725 }
726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 return handled;
728 }
729
730 /**
731 * Returns <code>true</code> if the amount of text in the field meets
732 * or exceeds the {@link #getThreshold} requirement. You can override
733 * this to impose a different standard for when filtering will be
734 * triggered.
735 */
736 public boolean enoughToFilter() {
737 if (DEBUG) Log.v(TAG, "Enough to filter: len=" + getText().length()
738 + " threshold=" + mThreshold);
739 return getText().length() >= mThreshold;
740 }
741
742 /**
743 * This is used to watch for edits to the text view. Note that we call
744 * to methods on the auto complete text view class so that we can access
745 * private vars without going through thunks.
746 */
747 private class MyWatcher implements TextWatcher {
748 public void afterTextChanged(Editable s) {
749 doAfterTextChanged();
750 }
751 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
752 doBeforeTextChanged();
753 }
754 public void onTextChanged(CharSequence s, int start, int before, int count) {
755 }
756 }
757
758 void doBeforeTextChanged() {
759 if (mBlockCompletion) return;
760
761 // when text is changed, inserted or deleted, we attempt to show
762 // the drop down
763 mOpenBefore = isPopupShowing();
764 if (DEBUG) Log.v(TAG, "before text changed: open=" + mOpenBefore);
765 }
766
767 void doAfterTextChanged() {
768 if (mBlockCompletion) return;
769
770 // if the list was open before the keystroke, but closed afterwards,
771 // then something in the keystroke processing (an input filter perhaps)
772 // called performCompletion() and we shouldn't do any more processing.
773 if (DEBUG) Log.v(TAG, "after text changed: openBefore=" + mOpenBefore
774 + " open=" + isPopupShowing());
775 if (mOpenBefore && !isPopupShowing()) {
776 return;
777 }
778
779 // the drop down is shown only when a minimum number of characters
780 // was typed in the text view
781 if (enoughToFilter()) {
782 if (mFilter != null) {
Gilles Debunne4a74dbc2011-01-23 17:23:31 -0800783 mPopupCanBeUpdated = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 performFiltering(getText(), mLastKeyCode);
785 }
786 } else {
787 // drop down is automatically dismissed when enough characters
788 // are deleted from the text view
Gilles Debunne4a74dbc2011-01-23 17:23:31 -0800789 if (!mPopup.isDropDownAlwaysVisible()) {
790 dismissDropDown();
791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 if (mFilter != null) {
793 mFilter.filter(null);
794 }
795 }
796 }
797
798 /**
799 * <p>Indicates whether the popup menu is showing.</p>
800 *
801 * @return true if the popup menu is showing, false otherwise
802 */
803 public boolean isPopupShowing() {
804 return mPopup.isShowing();
805 }
806
807 /**
808 * <p>Converts the selected item from the drop down list into a sequence
809 * of character that can be used in the edit box.</p>
810 *
811 * @param selectedItem the item selected by the user for completion
812 *
813 * @return a sequence of characters representing the selected suggestion
814 */
815 protected CharSequence convertSelectionToString(Object selectedItem) {
816 return mFilter.convertResultToString(selectedItem);
817 }
818
819 /**
820 * <p>Clear the list selection. This may only be temporary, as user input will often bring
821 * it back.
822 */
823 public void clearListSelection() {
Adam Powellc3fa6302010-05-18 11:36:27 -0700824 mPopup.clearListSelection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 }
826
827 /**
828 * Set the position of the dropdown view selection.
829 *
830 * @param position The position to move the selector to.
831 */
832 public void setListSelection(int position) {
Adam Powellc3fa6302010-05-18 11:36:27 -0700833 mPopup.setSelection(position);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 }
835
836 /**
837 * Get the position of the dropdown view selection, if there is one. Returns
838 * {@link ListView#INVALID_POSITION ListView.INVALID_POSITION} if there is no dropdown or if
839 * there is no selection.
840 *
841 * @return the position of the current selection, if there is one, or
842 * {@link ListView#INVALID_POSITION ListView.INVALID_POSITION} if not.
843 *
844 * @see ListView#getSelectedItemPosition()
845 */
846 public int getListSelection() {
Adam Powellc3fa6302010-05-18 11:36:27 -0700847 return mPopup.getSelectedItemPosition();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 }
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700849
850 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 * <p>Starts filtering the content of the drop down list. The filtering
852 * pattern is the content of the edit box. Subclasses should override this
853 * method to filter with a different pattern, for instance a substring of
854 * <code>text</code>.</p>
855 *
856 * @param text the filtering pattern
857 * @param keyCode the last character inserted in the edit box; beware that
858 * this will be null when text is being added through a soft input method.
859 */
860 @SuppressWarnings({ "UnusedDeclaration" })
861 protected void performFiltering(CharSequence text, int keyCode) {
862 mFilter.filter(text, this);
863 }
864
865 /**
866 * <p>Performs the text completion by converting the selected item from
867 * the drop down list into a string, replacing the text box's content with
868 * this string and finally dismissing the drop down menu.</p>
869 */
870 public void performCompletion() {
871 performCompletion(null, -1, -1);
872 }
873
874 @Override
875 public void onCommitCompletion(CompletionInfo completion) {
876 if (isPopupShowing()) {
Adam Powellc3fa6302010-05-18 11:36:27 -0700877 mPopup.performItemClick(completion.getPosition());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879 }
880
881 private void performCompletion(View selectedView, int position, long id) {
882 if (isPopupShowing()) {
883 Object selectedItem;
884 if (position < 0) {
Adam Powellc3fa6302010-05-18 11:36:27 -0700885 selectedItem = mPopup.getSelectedItem();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 } else {
887 selectedItem = mAdapter.getItem(position);
888 }
889 if (selectedItem == null) {
890 Log.w(TAG, "performCompletion: no selected item");
891 return;
892 }
893
894 mBlockCompletion = true;
895 replaceText(convertSelectionToString(selectedItem));
Gilles Debunnebe2c4f92011-01-17 15:14:32 -0800896 mBlockCompletion = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897
898 if (mItemClickListener != null) {
Adam Powellc3fa6302010-05-18 11:36:27 -0700899 final ListPopupWindow list = mPopup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900
901 if (selectedView == null || position < 0) {
902 selectedView = list.getSelectedView();
903 position = list.getSelectedItemPosition();
904 id = list.getSelectedItemId();
905 }
Adam Powellc3fa6302010-05-18 11:36:27 -0700906 mItemClickListener.onItemClick(list.getListView(), selectedView, position, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 }
908 }
909
Adam Powellc3fa6302010-05-18 11:36:27 -0700910 if (mDropDownDismissedOnCompletion && !mPopup.isDropDownAlwaysVisible()) {
Karl Rosaen875d50a2009-04-23 19:00:21 -0700911 dismissDropDown();
912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914
915 /**
916 * Identifies whether the view is currently performing a text completion, so subclasses
917 * can decide whether to respond to text changed events.
918 */
919 public boolean isPerformingCompletion() {
920 return mBlockCompletion;
921 }
922
923 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700924 * Like {@link #setText(CharSequence)}, except that it can disable filtering.
925 *
926 * @param filter If <code>false</code>, no filtering will be performed
927 * as a result of this call.
Karl Rosaen875d50a2009-04-23 19:00:21 -0700928 */
929 public void setText(CharSequence text, boolean filter) {
930 if (filter) {
931 setText(text);
932 } else {
933 mBlockCompletion = true;
934 setText(text);
935 mBlockCompletion = false;
936 }
937 }
Romain Guy6a678102010-03-16 13:49:31 -0700938
Karl Rosaen875d50a2009-04-23 19:00:21 -0700939 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 * <p>Performs the text completion by replacing the current text by the
941 * selected item. Subclasses should override this method to avoid replacing
942 * the whole content of the edit box.</p>
943 *
944 * @param text the selected suggestion in the drop down list
945 */
946 protected void replaceText(CharSequence text) {
Daisuke Miyakawac1d27482009-05-25 17:37:41 +0900947 clearComposingText();
948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 setText(text);
950 // make sure we keep the caret at the end of the text view
951 Editable spannable = getText();
952 Selection.setSelection(spannable, spannable.length());
953 }
954
Karl Rosaen875d50a2009-04-23 19:00:21 -0700955 /** {@inheritDoc} */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 public void onFilterComplete(int count) {
Gilles Debunne4a74dbc2011-01-23 17:23:31 -0800957 updateDropDownForFilter(count);
Romain Guy4f43ae02010-03-17 10:31:15 -0700958 }
959
Gilles Debunne4a74dbc2011-01-23 17:23:31 -0800960 private void updateDropDownForFilter(int count) {
Bjorn Bringert50145bc2009-06-11 12:30:48 +0100961 // Not attached to window, don't update drop-down
962 if (getWindowVisibility() == View.GONE) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963
964 /*
965 * This checks enoughToFilter() again because filtering requests
966 * are asynchronous, so the result may come back after enough text
967 * has since been deleted to make it no longer appropriate
968 * to filter.
969 */
970
Adam Powellc3fa6302010-05-18 11:36:27 -0700971 final boolean dropDownAlwaysVisible = mPopup.isDropDownAlwaysVisible();
Gilles Debunned513e972011-09-14 15:00:51 -0700972 final boolean enoughToFilter = enoughToFilter();
973 if ((count > 0 || dropDownAlwaysVisible) && enoughToFilter) {
Gilles Debunne4a74dbc2011-01-23 17:23:31 -0800974 if (hasFocus() && hasWindowFocus() && mPopupCanBeUpdated) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 showDropDown();
976 }
Gilles Debunne4a74dbc2011-01-23 17:23:31 -0800977 } else if (!dropDownAlwaysVisible && isPopupShowing()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 dismissDropDown();
Gilles Debunne4a74dbc2011-01-23 17:23:31 -0800979 // When the filter text is changed, the first update from the adapter may show an empty
980 // count (when the query is being performed on the network). Future updates when some
981 // content has been retrieved should still be able to update the list.
982 mPopupCanBeUpdated = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984 }
985
986 @Override
987 public void onWindowFocusChanged(boolean hasWindowFocus) {
988 super.onWindowFocusChanged(hasWindowFocus);
Adam Powellc3fa6302010-05-18 11:36:27 -0700989 if (!hasWindowFocus && !mPopup.isDropDownAlwaysVisible()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 dismissDropDown();
991 }
992 }
993
994 @Override
Romain Guy43c9cdf2010-01-27 13:53:55 -0800995 protected void onDisplayHint(int hint) {
996 super.onDisplayHint(hint);
997 switch (hint) {
998 case INVISIBLE:
Adam Powellc3fa6302010-05-18 11:36:27 -0700999 if (!mPopup.isDropDownAlwaysVisible()) {
Romain Guy43c9cdf2010-01-27 13:53:55 -08001000 dismissDropDown();
1001 }
1002 break;
1003 }
1004 }
1005
1006 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
1008 super.onFocusChanged(focused, direction, previouslyFocusedRect);
Evan Millar280c7532009-11-11 14:59:22 -08001009 // Perform validation if the view is losing focus.
1010 if (!focused) {
1011 performValidation();
1012 }
Adam Powellc3fa6302010-05-18 11:36:27 -07001013 if (!focused && !mPopup.isDropDownAlwaysVisible()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 dismissDropDown();
1015 }
1016 }
1017
1018 @Override
1019 protected void onAttachedToWindow() {
1020 super.onAttachedToWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
1022
1023 @Override
1024 protected void onDetachedFromWindow() {
1025 dismissDropDown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 super.onDetachedFromWindow();
1027 }
1028
1029 /**
1030 * <p>Closes the drop down if present on screen.</p>
1031 */
1032 public void dismissDropDown() {
1033 InputMethodManager imm = InputMethodManager.peekInstance();
1034 if (imm != null) {
1035 imm.displayCompletions(this, null);
1036 }
1037 mPopup.dismiss();
Gilles Debunne4a74dbc2011-01-23 17:23:31 -08001038 mPopupCanBeUpdated = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 }
1040
1041 @Override
Romain Guy3e141682010-03-08 17:44:40 -08001042 protected boolean setFrame(final int l, int t, final int r, int b) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 boolean result = super.setFrame(l, t, r, b);
1044
Gilles Debunne711734a2011-02-07 18:26:11 -08001045 if (isPopupShowing()) {
Romain Guy3e141682010-03-08 17:44:40 -08001046 showDropDown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048
1049 return result;
1050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051
1052 /**
Satish Sampathfef8d3e2009-07-01 17:48:42 +01001053 * Issues a runnable to show the dropdown as soon as possible.
Satish Sampath1b1a6e42009-07-01 19:06:24 +01001054 *
Mike LeBeauffe3ecf2009-07-16 18:18:37 -07001055 * @hide internal used only by SearchDialog
Satish Sampathfef8d3e2009-07-01 17:48:42 +01001056 */
1057 public void showDropDownAfterLayout() {
Adam Powellc3fa6302010-05-18 11:36:27 -07001058 mPopup.postShow();
Satish Sampathfef8d3e2009-07-01 17:48:42 +01001059 }
Mike LeBeauffe3ecf2009-07-16 18:18:37 -07001060
1061 /**
1062 * Ensures that the drop down is not obscuring the IME.
Amith Yamasanid25eb352010-03-10 21:09:28 -08001063 * @param visible whether the ime should be in front. If false, the ime is pushed to
1064 * the background.
Mike LeBeauffe3ecf2009-07-16 18:18:37 -07001065 * @hide internal used only here and SearchDialog
1066 */
Amith Yamasanid25eb352010-03-10 21:09:28 -08001067 public void ensureImeVisible(boolean visible) {
1068 mPopup.setInputMethodMode(visible
Adam Powellc3fa6302010-05-18 11:36:27 -07001069 ? ListPopupWindow.INPUT_METHOD_NEEDED : ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
Romain Guyc27cc012012-02-15 18:34:37 -08001070 if (mPopup.isDropDownAlwaysVisible() || (mFilter != null && enoughToFilter())) {
1071 showDropDown();
1072 }
Mike LeBeauffe3ecf2009-07-16 18:18:37 -07001073 }
Satish Sampathfef8d3e2009-07-01 17:48:42 +01001074
1075 /**
Bjorn Bringert003ad482009-07-28 12:03:29 +01001076 * @hide internal used only here and SearchDialog
1077 */
1078 public boolean isInputMethodNotNeeded() {
Adam Powellc3fa6302010-05-18 11:36:27 -07001079 return mPopup.getInputMethodMode() == ListPopupWindow.INPUT_METHOD_NOT_NEEDED;
Bjorn Bringert003ad482009-07-28 12:03:29 +01001080 }
1081
1082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 * <p>Displays the drop down on screen.</p>
1084 */
1085 public void showDropDown() {
Gilles Debunned513e972011-09-14 15:00:51 -07001086 buildImeCompletions();
1087
Adam Powellc3fa6302010-05-18 11:36:27 -07001088 if (mPopup.getAnchorView() == null) {
1089 if (mDropDownAnchorId != View.NO_ID) {
1090 mPopup.setAnchorView(getRootView().findViewById(mDropDownAnchorId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 } else {
Adam Powellc3fa6302010-05-18 11:36:27 -07001092 mPopup.setAnchorView(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
Gilles Debunne711734a2011-02-07 18:26:11 -08001095 if (!isPopupShowing()) {
1096 // Make sure the list does not obscure the IME when shown for the first time.
1097 mPopup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED);
Adam Powell348e69c2011-02-16 16:49:50 -08001098 mPopup.setListItemExpandMax(EXPAND_MAX);
Gilles Debunne711734a2011-02-07 18:26:11 -08001099 }
Adam Powellc3fa6302010-05-18 11:36:27 -07001100 mPopup.show();
Adam Powell348e69c2011-02-16 16:49:50 -08001101 mPopup.getListView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
Gilles Debunned513e972011-09-14 15:00:51 -07001103
Mike LeBeaud4760d72009-07-22 15:04:27 -07001104 /**
1105 * Forces outside touches to be ignored. Normally if {@link #isDropDownAlwaysVisible()} is
1106 * false, we allow outside touch to dismiss the dropdown. If this is set to true, then we
1107 * ignore outside touch even when the drop down is not set to always visible.
1108 *
1109 * @hide used only by SearchDialog
1110 */
1111 public void setForceIgnoreOutsideTouch(boolean forceIgnoreOutsideTouch) {
Adam Powellc3fa6302010-05-18 11:36:27 -07001112 mPopup.setForceIgnoreOutsideTouch(forceIgnoreOutsideTouch);
Mike LeBeaud4760d72009-07-22 15:04:27 -07001113 }
Gilles Debunned513e972011-09-14 15:00:51 -07001114
Adam Powellc3fa6302010-05-18 11:36:27 -07001115 private void buildImeCompletions() {
Romain Guy4f43ae02010-03-17 10:31:15 -07001116 final ListAdapter adapter = mAdapter;
1117 if (adapter != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 InputMethodManager imm = InputMethodManager.peekInstance();
1119 if (imm != null) {
Romain Guy4f43ae02010-03-17 10:31:15 -07001120 final int count = Math.min(adapter.getCount(), 20);
1121 CompletionInfo[] completions = new CompletionInfo[count];
1122 int realCount = 0;
1123
1124 for (int i = 0; i < count; i++) {
1125 if (adapter.isEnabled(i)) {
Romain Guy4f43ae02010-03-17 10:31:15 -07001126 Object item = adapter.getItem(i);
1127 long id = adapter.getItemId(i);
Gilles Debunnef0d3b7f2012-01-18 18:02:46 -08001128 completions[realCount] = new CompletionInfo(id, realCount,
1129 convertSelectionToString(item));
1130 realCount++;
Romain Guy4f43ae02010-03-17 10:31:15 -07001131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 }
Romain Guy4f43ae02010-03-17 10:31:15 -07001133
1134 if (realCount != count) {
1135 CompletionInfo[] tmp = new CompletionInfo[realCount];
1136 System.arraycopy(completions, 0, tmp, 0, realCount);
1137 completions = tmp;
1138 }
1139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 imm.displayCompletions(this, completions);
1141 }
1142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
1144
1145 /**
1146 * Sets the validator used to perform text validation.
1147 *
1148 * @param validator The validator used to validate the text entered in this widget.
1149 *
1150 * @see #getValidator()
1151 * @see #performValidation()
1152 */
1153 public void setValidator(Validator validator) {
1154 mValidator = validator;
1155 }
1156
1157 /**
1158 * Returns the Validator set with {@link #setValidator},
1159 * or <code>null</code> if it was not set.
1160 *
1161 * @see #setValidator(android.widget.AutoCompleteTextView.Validator)
1162 * @see #performValidation()
1163 */
1164 public Validator getValidator() {
1165 return mValidator;
1166 }
1167
1168 /**
1169 * If a validator was set on this view and the current string is not valid,
1170 * ask the validator to fix it.
1171 *
1172 * @see #getValidator()
1173 * @see #setValidator(android.widget.AutoCompleteTextView.Validator)
1174 */
1175 public void performValidation() {
1176 if (mValidator == null) return;
1177
1178 CharSequence text = getText();
1179
1180 if (!TextUtils.isEmpty(text) && !mValidator.isValid(text)) {
1181 setText(mValidator.fixText(text));
1182 }
1183 }
1184
1185 /**
1186 * Returns the Filter obtained from {@link Filterable#getFilter},
1187 * or <code>null</code> if {@link #setAdapter} was not called with
1188 * a Filterable.
1189 */
1190 protected Filter getFilter() {
1191 return mFilter;
1192 }
1193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 private class DropDownItemClickListener implements AdapterView.OnItemClickListener {
1195 public void onItemClick(AdapterView parent, View v, int position, long id) {
1196 performCompletion(v, position, id);
1197 }
1198 }
1199
1200 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 * This interface is used to make sure that the text entered in this TextView complies to
1202 * a certain format. Since there is no foolproof way to prevent the user from leaving
1203 * this View with an incorrect value in it, all we can do is try to fix it ourselves
1204 * when this happens.
1205 */
1206 public interface Validator {
1207 /**
1208 * Validates the specified text.
1209 *
1210 * @return true If the text currently in the text editor is valid.
1211 *
1212 * @see #fixText(CharSequence)
1213 */
1214 boolean isValid(CharSequence text);
1215
1216 /**
1217 * Corrects the specified text to make it valid.
1218 *
1219 * @param invalidText A string that doesn't pass validation: isValid(invalidText)
1220 * returns false
1221 *
1222 * @return A string based on invalidText such as invoking isValid() on it returns true.
1223 *
1224 * @see #isValid(CharSequence)
1225 */
1226 CharSequence fixText(CharSequence invalidText);
1227 }
Mike LeBeauddf98562009-05-08 17:59:05 -07001228
Karl Rosaen98e333f2009-04-28 10:39:09 -07001229 /**
Adam Powell780c4912012-08-30 17:30:05 -07001230 * Listener to respond to the AutoCompleteTextView's completion list being dismissed.
1231 * @see AutoCompleteTextView#setOnDismissListener(OnDismissListener)
1232 */
1233 public interface OnDismissListener {
1234 /**
1235 * This method will be invoked whenever the AutoCompleteTextView's list
1236 * of completion options has been dismissed and is no longer available
1237 * for user interaction.
1238 */
1239 void onDismiss();
1240 }
1241
1242 /**
Karl Rosaen98e333f2009-04-28 10:39:09 -07001243 * Allows us a private hook into the on click event without preventing users from setting
1244 * their own click listener.
1245 */
1246 private class PassThroughClickListener implements OnClickListener {
1247
1248 private View.OnClickListener mWrapped;
1249
1250 /** {@inheritDoc} */
1251 public void onClick(View v) {
1252 onClickImpl();
1253
1254 if (mWrapped != null) mWrapped.onClick(v);
1255 }
1256 }
Romain Guy6a678102010-03-16 13:49:31 -07001257
1258 private class PopupDataSetObserver extends DataSetObserver {
1259 @Override
1260 public void onChanged() {
Adam Powellc3fa6302010-05-18 11:36:27 -07001261 if (mAdapter != null) {
Romain Guy4f43ae02010-03-17 10:31:15 -07001262 // If the popup is not showing already, showing it will cause
1263 // the list of data set observers attached to the adapter to
1264 // change. We can't do it from here, because we are in the middle
Gilles Debunnebe2c4f92011-01-17 15:14:32 -08001265 // of iterating through the list of observers.
Romain Guy4f43ae02010-03-17 10:31:15 -07001266 post(new Runnable() {
1267 public void run() {
Kenny Root7254afd2010-03-19 10:09:59 -07001268 final ListAdapter adapter = mAdapter;
1269 if (adapter != null) {
Gilles Debunnebe2c4f92011-01-17 15:14:32 -08001270 // This will re-layout, thus resetting mDataChanged, so that the
1271 // listView click listener stays responsive
Gilles Debunne4a74dbc2011-01-23 17:23:31 -08001272 updateDropDownForFilter(adapter.getCount());
Kenny Root7254afd2010-03-19 10:09:59 -07001273 }
Romain Guy4f43ae02010-03-17 10:31:15 -07001274 }
1275 });
Romain Guy6a678102010-03-16 13:49:31 -07001276 }
1277 }
Romain Guy6a678102010-03-16 13:49:31 -07001278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279}