The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.widget; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
Romain Guy | 6a67810 | 2010-03-16 13:49:31 -0700 | [diff] [blame] | 21 | import android.database.DataSetObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.graphics.Rect; |
| 23 | import android.graphics.drawable.Drawable; |
| 24 | import android.text.Editable; |
| 25 | import android.text.Selection; |
| 26 | import android.text.TextUtils; |
| 27 | import android.text.TextWatcher; |
| 28 | import android.util.AttributeSet; |
| 29 | import android.util.Log; |
| 30 | import android.view.KeyEvent; |
| 31 | import android.view.LayoutInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.view.View; |
| 33 | import android.view.ViewGroup; |
Romain Guy | 374aaaed3 | 2009-07-14 15:11:59 -0700 | [diff] [blame] | 34 | import android.view.WindowManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.view.inputmethod.CompletionInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.view.inputmethod.EditorInfo; |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 37 | import android.view.inputmethod.InputMethodManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
Gilles Debunne | 688df79 | 2011-09-14 15:52:03 -0700 | [diff] [blame] | 39 | import com.android.internal.R; |
| 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 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<String> adapter = new ArrayAdapter<String>(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 Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 78 | * <p>See the <a href="{@docRoot}guide/topics/ui/controls/text.html">Text Fields</a> |
| 79 | * guide.</p> |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 80 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | * @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 Guy | e29f064 | 2009-06-23 21:27:02 -0700 | [diff] [blame] | 87 | * @attr ref android.R.styleable#AutoCompleteTextView_dropDownHeight |
Romain Guy | aa1c631 | 2009-09-30 11:36:01 -0700 | [diff] [blame] | 88 | * @attr ref android.R.styleable#AutoCompleteTextView_dropDownVerticalOffset |
| 89 | * @attr ref android.R.styleable#AutoCompleteTextView_dropDownHorizontalOffset |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | */ |
| 91 | public class AutoCompleteTextView extends EditText implements Filter.FilterListener { |
| 92 | static final boolean DEBUG = false; |
| 93 | static final String TAG = "AutoCompleteTextView"; |
| 94 | |
Adam Powell | 348e69c | 2011-02-16 16:49:50 -0800 | [diff] [blame] | 95 | static final int EXPAND_MAX = 3; |
| 96 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | private CharSequence mHintText; |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 98 | private TextView mHintView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | private int mHintResource; |
| 100 | |
| 101 | private ListAdapter mAdapter; |
| 102 | private Filter mFilter; |
| 103 | private int mThreshold; |
| 104 | |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 105 | private ListPopupWindow mPopup; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | private int mDropDownAnchorId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | |
| 108 | private AdapterView.OnItemClickListener mItemClickListener; |
| 109 | private AdapterView.OnItemSelectedListener mItemSelectedListener; |
| 110 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 111 | private boolean mDropDownDismissedOnCompletion = true; |
| 112 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | private int mLastKeyCode = KeyEvent.KEYCODE_UNKNOWN; |
| 114 | private boolean mOpenBefore; |
| 115 | |
| 116 | private Validator mValidator = null; |
| 117 | |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 118 | // Set to true when text is set directly and no filtering shall be performed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | private boolean mBlockCompletion; |
| 120 | |
Gilles Debunne | 4a74dbc | 2011-01-23 17:23:31 -0800 | [diff] [blame] | 121 | // 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 Guy | 6a67810 | 2010-03-16 13:49:31 -0700 | [diff] [blame] | 125 | private PassThroughClickListener mPassThroughClickListener; |
| 126 | private PopupDataSetObserver mObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | |
| 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 Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 136 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 144 | mPopup = new ListPopupWindow(context, attrs, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | com.android.internal.R.attr.autoCompleteTextViewStyle); |
Romain Guy | 374aaaed3 | 2009-07-14 15:11:59 -0700 | [diff] [blame] | 146 | mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 147 | mPopup.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 149 | final TypedArray a = context.obtainStyledAttributes(attrs, |
| 150 | com.android.internal.R.styleable.AutoCompleteTextView, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | |
| 152 | mThreshold = a.getInt( |
| 153 | R.styleable.AutoCompleteTextView_completionThreshold, 2); |
| 154 | |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 155 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | |
| 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 Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 168 | // For dropdown width, the developer can specify a specific width, or MATCH_PARENT |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | // (for full screen width) or WRAP_CONTENT (to match the width of the anchored view). |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 170 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | |
| 177 | mHintResource = a.getResourceId(R.styleable.AutoCompleteTextView_completionHintView, |
| 178 | R.layout.simple_dropdown_hint); |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 179 | |
| 180 | mPopup.setOnItemClickListener(new DropDownItemClickListener()); |
| 181 | setCompletionHint(a.getText(R.styleable.AutoCompleteTextView_completionHint)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | |
| 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 LeBeau | ddf9856 | 2009-05-08 17:59:05 -0700 | [diff] [blame] | 197 | |
Karl Rosaen | 98e333f | 2009-04-28 10:39:09 -0700 | [diff] [blame] | 198 | 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 Yamasani | d25eb35 | 2010-03-10 21:09:28 -0800 | [diff] [blame] | 211 | // If the dropdown is showing, bring the keyboard to the front |
| 212 | // when the user touches the text field. |
Gilles Debunne | 711734a | 2011-02-07 18:26:11 -0800 | [diff] [blame] | 213 | if (isPopupShowing()) { |
Amith Yamasani | d25eb35 | 2010-03-10 21:09:28 -0800 | [diff] [blame] | 214 | ensureImeVisible(true); |
Karl Rosaen | 98e333f | 2009-04-28 10:39:09 -0700 | [diff] [blame] | 215 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | * <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 Debunne | 5c49d11 | 2012-04-30 16:44:14 -0700 | [diff] [blame] | 225 | * @see #getCompletionHint() |
| 226 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | * @attr ref android.R.styleable#AutoCompleteTextView_completionHint |
| 228 | */ |
| 229 | public void setCompletionHint(CharSequence hint) { |
| 230 | mHintText = hint; |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 231 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | } |
Gilles Debunne | 5c49d11 | 2012-04-30 16:44:14 -0700 | [diff] [blame] | 246 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | /** |
| 261 | * <p>Returns the current width for the auto-complete drop down list. This can |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 262 | * be a fixed width, or {@link ViewGroup.LayoutParams#MATCH_PARENT} to fill the screen, or |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | * {@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 Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 266 | * |
| 267 | * @attr ref android.R.styleable#AutoCompleteTextView_dropDownWidth |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | */ |
| 269 | public int getDropDownWidth() { |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 270 | return mPopup.getWidth(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
| 274 | * <p>Sets the current width for the auto-complete drop down list. This can |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 275 | * be a fixed width, or {@link ViewGroup.LayoutParams#MATCH_PARENT} to fill the screen, or |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | * {@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 Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 279 | * |
| 280 | * @attr ref android.R.styleable#AutoCompleteTextView_dropDownWidth |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | */ |
| 282 | public void setDropDownWidth(int width) { |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 283 | mPopup.setWidth(width); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | } |
Romain Guy | e29f064 | 2009-06-23 21:27:02 -0700 | [diff] [blame] | 285 | |
| 286 | /** |
| 287 | * <p>Returns the current height for the auto-complete drop down list. This can |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 288 | * be a fixed height, or {@link ViewGroup.LayoutParams#MATCH_PARENT} to fill |
Romain Guy | e29f064 | 2009-06-23 21:27:02 -0700 | [diff] [blame] | 289 | * 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 297 | return mPopup.getHeight(); |
Romain Guy | e29f064 | 2009-06-23 21:27:02 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /** |
| 301 | * <p>Sets the current height for the auto-complete drop down list. This can |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 302 | * be a fixed height, or {@link ViewGroup.LayoutParams#MATCH_PARENT} to fill |
Romain Guy | e29f064 | 2009-06-23 21:27:02 -0700 | [diff] [blame] | 303 | * 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 311 | mPopup.setHeight(height); |
Romain Guy | e29f064 | 2009-06-23 21:27:02 -0700 | [diff] [blame] | 312 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | |
| 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 Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 318 | * |
| 319 | * @attr ref android.R.styleable#AutoCompleteTextView_dropDownAnchor |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | */ |
| 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 Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 331 | * |
| 332 | * @attr ref android.R.styleable#AutoCompleteTextView_dropDownAnchor |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | */ |
| 334 | public void setDropDownAnchor(int id) { |
| 335 | mDropDownAnchorId = id; |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 336 | mPopup.setAnchorView(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | } |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 338 | |
| 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 Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 369 | mPopup.setBackgroundDrawable(getContext().getDrawable(id)); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 370 | } |
| 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 378 | mPopup.setVerticalOffset(offset); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 379 | } |
| 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 387 | return mPopup.getVerticalOffset(); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 388 | } |
| 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 396 | mPopup.setHorizontalOffset(offset); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 397 | } |
| 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 405 | return mPopup.getHorizontalOffset(); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 406 | } |
The Android Open Source Project | 8f080ec | 2009-04-29 13:34:51 -0700 | [diff] [blame] | 407 | |
The Android Open Source Project | bff1389 | 2009-04-29 13:41:02 -0700 | [diff] [blame] | 408 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 409 | * <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 Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 435 | |
| 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 442 | return mPopup.isDropDownAlwaysVisible(); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 443 | } |
| 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 459 | mPopup.setDropDownAlwaysVisible(dropDownAlwaysVisible); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 460 | } |
| 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 Guy | 6a67810 | 2010-03-16 13:49:31 -0700 | [diff] [blame] | 470 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 471 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | /** |
| 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 Debunne | 5c49d11 | 2012-04-30 16:44:14 -0700 | [diff] [blame] | 490 | * |
| 491 | * @attr ref android.R.styleable#AutoCompleteTextView_completionThreshold |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | */ |
| 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 Powell | 780c491 | 2012-08-30 17:30:05 -0700 | [diff] [blame] | 586 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | * <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 Guy | 6a67810 | 2010-03-16 13:49:31 -0700 | [diff] [blame] | 631 | if (mObserver == null) { |
| 632 | mObserver = new PopupDataSetObserver(); |
| 633 | } else if (mAdapter != null) { |
| 634 | mAdapter.unregisterDataSetObserver(mObserver); |
| 635 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | mAdapter = adapter; |
| 637 | if (mAdapter != null) { |
| 638 | //noinspection unchecked |
| 639 | mFilter = ((Filterable) mAdapter).getFilter(); |
Romain Guy | 6a67810 | 2010-03-16 13:49:31 -0700 | [diff] [blame] | 640 | adapter.registerDataSetObserver(mObserver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | } else { |
| 642 | mFilter = null; |
| 643 | } |
| 644 | |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 645 | mPopup.setAdapter(mAdapter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | @Override |
| 649 | public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 650 | if (keyCode == KeyEvent.KEYCODE_BACK && isPopupShowing() |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 651 | && !mPopup.isDropDownAlwaysVisible()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | // 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 Guy | 2beee4d | 2010-03-15 17:18:22 -0700 | [diff] [blame] | 654 | if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { |
Jeff Brown | b3ea922 | 2011-01-10 16:26:36 -0800 | [diff] [blame] | 655 | KeyEvent.DispatcherState state = getKeyDispatcherState(); |
| 656 | if (state != null) { |
| 657 | state.startTracking(event, this); |
| 658 | } |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 659 | return true; |
Bjorn Bringert | 4eb3efc | 2009-10-06 09:26:06 +0100 | [diff] [blame] | 660 | } else if (event.getAction() == KeyEvent.ACTION_UP) { |
Jeff Brown | b3ea922 | 2011-01-10 16:26:36 -0800 | [diff] [blame] | 661 | KeyEvent.DispatcherState state = getKeyDispatcherState(); |
| 662 | if (state != null) { |
| 663 | state.handleUpEvent(event); |
| 664 | } |
Bjorn Bringert | 4eb3efc | 2009-10-06 09:26:06 +0100 | [diff] [blame] | 665 | if (event.isTracking() && !event.isCanceled()) { |
| 666 | dismissDropDown(); |
| 667 | return true; |
| 668 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | return super.onKeyPreIme(keyCode, event); |
| 672 | } |
| 673 | |
| 674 | @Override |
| 675 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 676 | 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 Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 684 | case KeyEvent.KEYCODE_TAB: |
| 685 | if (event.hasNoModifiers()) { |
| 686 | performCompletion(); |
| 687 | } |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 688 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | } |
| 690 | } |
Gilles Debunne | a4a5758 | 2011-02-08 18:21:01 -0800 | [diff] [blame] | 691 | |
| 692 | if (isPopupShowing() && keyCode == KeyEvent.KEYCODE_TAB && event.hasNoModifiers()) { |
| 693 | performCompletion(); |
| 694 | return true; |
| 695 | } |
| 696 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | return super.onKeyUp(keyCode, event); |
| 698 | } |
| 699 | |
| 700 | @Override |
| 701 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 702 | if (mPopup.onKeyDown(keyCode, event)) { |
| 703 | return true; |
| 704 | } |
| 705 | |
| 706 | if (!isPopupShowing()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | switch(keyCode) { |
| 708 | case KeyEvent.KEYCODE_DPAD_DOWN: |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 709 | if (event.hasNoModifiers()) { |
| 710 | performValidation(); |
| 711 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
Gilles Debunne | a4a5758 | 2011-02-08 18:21:01 -0800 | [diff] [blame] | 715 | if (isPopupShowing() && keyCode == KeyEvent.KEYCODE_TAB && event.hasNoModifiers()) { |
| 716 | return true; |
| 717 | } |
| 718 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | mLastKeyCode = keyCode; |
| 720 | boolean handled = super.onKeyDown(keyCode, event); |
| 721 | mLastKeyCode = KeyEvent.KEYCODE_UNKNOWN; |
| 722 | |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 723 | if (handled && isPopupShowing()) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 724 | clearListSelection(); |
| 725 | } |
| 726 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | 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 Debunne | 4a74dbc | 2011-01-23 17:23:31 -0800 | [diff] [blame] | 783 | mPopupCanBeUpdated = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | performFiltering(getText(), mLastKeyCode); |
| 785 | } |
| 786 | } else { |
| 787 | // drop down is automatically dismissed when enough characters |
| 788 | // are deleted from the text view |
Gilles Debunne | 4a74dbc | 2011-01-23 17:23:31 -0800 | [diff] [blame] | 789 | if (!mPopup.isDropDownAlwaysVisible()) { |
| 790 | dismissDropDown(); |
| 791 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 824 | mPopup.clearListSelection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | } |
| 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 833 | mPopup.setSelection(position); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | } |
| 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 847 | return mPopup.getSelectedItemPosition(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | } |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 849 | |
| 850 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | * <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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 877 | mPopup.performItemClick(completion.getPosition()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
| 881 | private void performCompletion(View selectedView, int position, long id) { |
| 882 | if (isPopupShowing()) { |
| 883 | Object selectedItem; |
| 884 | if (position < 0) { |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 885 | selectedItem = mPopup.getSelectedItem(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | } 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 Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 896 | mBlockCompletion = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | |
| 898 | if (mItemClickListener != null) { |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 899 | final ListPopupWindow list = mPopup; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | |
| 901 | if (selectedView == null || position < 0) { |
| 902 | selectedView = list.getSelectedView(); |
| 903 | position = list.getSelectedItemPosition(); |
| 904 | id = list.getSelectedItemId(); |
| 905 | } |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 906 | mItemClickListener.onItemClick(list.getListView(), selectedView, position, id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 910 | if (mDropDownDismissedOnCompletion && !mPopup.isDropDownAlwaysVisible()) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 911 | dismissDropDown(); |
| 912 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | } |
| 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 Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 924 | * 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 Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 928 | */ |
| 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 Guy | 6a67810 | 2010-03-16 13:49:31 -0700 | [diff] [blame] | 938 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 939 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | * <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 Miyakawa | c1d2748 | 2009-05-25 17:37:41 +0900 | [diff] [blame] | 947 | clearComposingText(); |
| 948 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | 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 Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 955 | /** {@inheritDoc} */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | public void onFilterComplete(int count) { |
Gilles Debunne | 4a74dbc | 2011-01-23 17:23:31 -0800 | [diff] [blame] | 957 | updateDropDownForFilter(count); |
Romain Guy | 4f43ae0 | 2010-03-17 10:31:15 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Gilles Debunne | 4a74dbc | 2011-01-23 17:23:31 -0800 | [diff] [blame] | 960 | private void updateDropDownForFilter(int count) { |
Bjorn Bringert | 50145bc | 2009-06-11 12:30:48 +0100 | [diff] [blame] | 961 | // Not attached to window, don't update drop-down |
| 962 | if (getWindowVisibility() == View.GONE) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | |
| 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 971 | final boolean dropDownAlwaysVisible = mPopup.isDropDownAlwaysVisible(); |
Gilles Debunne | d513e97 | 2011-09-14 15:00:51 -0700 | [diff] [blame] | 972 | final boolean enoughToFilter = enoughToFilter(); |
| 973 | if ((count > 0 || dropDownAlwaysVisible) && enoughToFilter) { |
Gilles Debunne | 4a74dbc | 2011-01-23 17:23:31 -0800 | [diff] [blame] | 974 | if (hasFocus() && hasWindowFocus() && mPopupCanBeUpdated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | showDropDown(); |
| 976 | } |
Gilles Debunne | 4a74dbc | 2011-01-23 17:23:31 -0800 | [diff] [blame] | 977 | } else if (!dropDownAlwaysVisible && isPopupShowing()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | dismissDropDown(); |
Gilles Debunne | 4a74dbc | 2011-01-23 17:23:31 -0800 | [diff] [blame] | 979 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
| 986 | @Override |
| 987 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 988 | super.onWindowFocusChanged(hasWindowFocus); |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 989 | if (!hasWindowFocus && !mPopup.isDropDownAlwaysVisible()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | dismissDropDown(); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | @Override |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 995 | protected void onDisplayHint(int hint) { |
| 996 | super.onDisplayHint(hint); |
| 997 | switch (hint) { |
| 998 | case INVISIBLE: |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 999 | if (!mPopup.isDropDownAlwaysVisible()) { |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 1000 | dismissDropDown(); |
| 1001 | } |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { |
| 1008 | super.onFocusChanged(focused, direction, previouslyFocusedRect); |
Evan Millar | 280c753 | 2009-11-11 14:59:22 -0800 | [diff] [blame] | 1009 | // Perform validation if the view is losing focus. |
| 1010 | if (!focused) { |
| 1011 | performValidation(); |
| 1012 | } |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 1013 | if (!focused && !mPopup.isDropDownAlwaysVisible()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | dismissDropDown(); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | @Override |
| 1019 | protected void onAttachedToWindow() { |
| 1020 | super.onAttachedToWindow(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | @Override |
| 1024 | protected void onDetachedFromWindow() { |
| 1025 | dismissDropDown(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1026 | 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 Debunne | 4a74dbc | 2011-01-23 17:23:31 -0800 | [diff] [blame] | 1038 | mPopupCanBeUpdated = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | @Override |
Romain Guy | 3e14168 | 2010-03-08 17:44:40 -0800 | [diff] [blame] | 1042 | protected boolean setFrame(final int l, int t, final int r, int b) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | boolean result = super.setFrame(l, t, r, b); |
| 1044 | |
Gilles Debunne | 711734a | 2011-02-07 18:26:11 -0800 | [diff] [blame] | 1045 | if (isPopupShowing()) { |
Romain Guy | 3e14168 | 2010-03-08 17:44:40 -0800 | [diff] [blame] | 1046 | showDropDown(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | return result; |
| 1050 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | |
| 1052 | /** |
Satish Sampath | fef8d3e | 2009-07-01 17:48:42 +0100 | [diff] [blame] | 1053 | * Issues a runnable to show the dropdown as soon as possible. |
Satish Sampath | 1b1a6e4 | 2009-07-01 19:06:24 +0100 | [diff] [blame] | 1054 | * |
Mike LeBeau | ffe3ecf | 2009-07-16 18:18:37 -0700 | [diff] [blame] | 1055 | * @hide internal used only by SearchDialog |
Satish Sampath | fef8d3e | 2009-07-01 17:48:42 +0100 | [diff] [blame] | 1056 | */ |
| 1057 | public void showDropDownAfterLayout() { |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 1058 | mPopup.postShow(); |
Satish Sampath | fef8d3e | 2009-07-01 17:48:42 +0100 | [diff] [blame] | 1059 | } |
Mike LeBeau | ffe3ecf | 2009-07-16 18:18:37 -0700 | [diff] [blame] | 1060 | |
| 1061 | /** |
| 1062 | * Ensures that the drop down is not obscuring the IME. |
Amith Yamasani | d25eb35 | 2010-03-10 21:09:28 -0800 | [diff] [blame] | 1063 | * @param visible whether the ime should be in front. If false, the ime is pushed to |
| 1064 | * the background. |
Mike LeBeau | ffe3ecf | 2009-07-16 18:18:37 -0700 | [diff] [blame] | 1065 | * @hide internal used only here and SearchDialog |
| 1066 | */ |
Amith Yamasani | d25eb35 | 2010-03-10 21:09:28 -0800 | [diff] [blame] | 1067 | public void ensureImeVisible(boolean visible) { |
| 1068 | mPopup.setInputMethodMode(visible |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 1069 | ? ListPopupWindow.INPUT_METHOD_NEEDED : ListPopupWindow.INPUT_METHOD_NOT_NEEDED); |
Romain Guy | c27cc01 | 2012-02-15 18:34:37 -0800 | [diff] [blame] | 1070 | if (mPopup.isDropDownAlwaysVisible() || (mFilter != null && enoughToFilter())) { |
| 1071 | showDropDown(); |
| 1072 | } |
Mike LeBeau | ffe3ecf | 2009-07-16 18:18:37 -0700 | [diff] [blame] | 1073 | } |
Satish Sampath | fef8d3e | 2009-07-01 17:48:42 +0100 | [diff] [blame] | 1074 | |
| 1075 | /** |
Bjorn Bringert | 003ad48 | 2009-07-28 12:03:29 +0100 | [diff] [blame] | 1076 | * @hide internal used only here and SearchDialog |
| 1077 | */ |
| 1078 | public boolean isInputMethodNotNeeded() { |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 1079 | return mPopup.getInputMethodMode() == ListPopupWindow.INPUT_METHOD_NOT_NEEDED; |
Bjorn Bringert | 003ad48 | 2009-07-28 12:03:29 +0100 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | * <p>Displays the drop down on screen.</p> |
| 1084 | */ |
| 1085 | public void showDropDown() { |
Gilles Debunne | d513e97 | 2011-09-14 15:00:51 -0700 | [diff] [blame] | 1086 | buildImeCompletions(); |
| 1087 | |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 1088 | if (mPopup.getAnchorView() == null) { |
| 1089 | if (mDropDownAnchorId != View.NO_ID) { |
| 1090 | mPopup.setAnchorView(getRootView().findViewById(mDropDownAnchorId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | } else { |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 1092 | mPopup.setAnchorView(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | } |
Gilles Debunne | 711734a | 2011-02-07 18:26:11 -0800 | [diff] [blame] | 1095 | 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 Powell | 348e69c | 2011-02-16 16:49:50 -0800 | [diff] [blame] | 1098 | mPopup.setListItemExpandMax(EXPAND_MAX); |
Gilles Debunne | 711734a | 2011-02-07 18:26:11 -0800 | [diff] [blame] | 1099 | } |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 1100 | mPopup.show(); |
Adam Powell | 348e69c | 2011-02-16 16:49:50 -0800 | [diff] [blame] | 1101 | mPopup.getListView().setOverScrollMode(View.OVER_SCROLL_ALWAYS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1102 | } |
Gilles Debunne | d513e97 | 2011-09-14 15:00:51 -0700 | [diff] [blame] | 1103 | |
Mike LeBeau | d4760d7 | 2009-07-22 15:04:27 -0700 | [diff] [blame] | 1104 | /** |
| 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 Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 1112 | mPopup.setForceIgnoreOutsideTouch(forceIgnoreOutsideTouch); |
Mike LeBeau | d4760d7 | 2009-07-22 15:04:27 -0700 | [diff] [blame] | 1113 | } |
Gilles Debunne | d513e97 | 2011-09-14 15:00:51 -0700 | [diff] [blame] | 1114 | |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 1115 | private void buildImeCompletions() { |
Romain Guy | 4f43ae0 | 2010-03-17 10:31:15 -0700 | [diff] [blame] | 1116 | final ListAdapter adapter = mAdapter; |
| 1117 | if (adapter != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1118 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 1119 | if (imm != null) { |
Romain Guy | 4f43ae0 | 2010-03-17 10:31:15 -0700 | [diff] [blame] | 1120 | 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 Guy | 4f43ae0 | 2010-03-17 10:31:15 -0700 | [diff] [blame] | 1126 | Object item = adapter.getItem(i); |
| 1127 | long id = adapter.getItemId(i); |
Gilles Debunne | f0d3b7f | 2012-01-18 18:02:46 -0800 | [diff] [blame] | 1128 | completions[realCount] = new CompletionInfo(id, realCount, |
| 1129 | convertSelectionToString(item)); |
| 1130 | realCount++; |
Romain Guy | 4f43ae0 | 2010-03-17 10:31:15 -0700 | [diff] [blame] | 1131 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1132 | } |
Romain Guy | 4f43ae0 | 2010-03-17 10:31:15 -0700 | [diff] [blame] | 1133 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1140 | imm.displayCompletions(this, completions); |
| 1141 | } |
| 1142 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1143 | } |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1201 | * 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 LeBeau | ddf9856 | 2009-05-08 17:59:05 -0700 | [diff] [blame] | 1228 | |
Karl Rosaen | 98e333f | 2009-04-28 10:39:09 -0700 | [diff] [blame] | 1229 | /** |
Adam Powell | 780c491 | 2012-08-30 17:30:05 -0700 | [diff] [blame] | 1230 | * 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 Rosaen | 98e333f | 2009-04-28 10:39:09 -0700 | [diff] [blame] | 1243 | * 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 Guy | 6a67810 | 2010-03-16 13:49:31 -0700 | [diff] [blame] | 1257 | |
| 1258 | private class PopupDataSetObserver extends DataSetObserver { |
| 1259 | @Override |
| 1260 | public void onChanged() { |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 1261 | if (mAdapter != null) { |
Romain Guy | 4f43ae0 | 2010-03-17 10:31:15 -0700 | [diff] [blame] | 1262 | // 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 Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 1265 | // of iterating through the list of observers. |
Romain Guy | 4f43ae0 | 2010-03-17 10:31:15 -0700 | [diff] [blame] | 1266 | post(new Runnable() { |
| 1267 | public void run() { |
Kenny Root | 7254afd | 2010-03-19 10:09:59 -0700 | [diff] [blame] | 1268 | final ListAdapter adapter = mAdapter; |
| 1269 | if (adapter != null) { |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 1270 | // This will re-layout, thus resetting mDataChanged, so that the |
| 1271 | // listView click listener stays responsive |
Gilles Debunne | 4a74dbc | 2011-01-23 17:23:31 -0800 | [diff] [blame] | 1272 | updateDropDownForFilter(adapter.getCount()); |
Kenny Root | 7254afd | 2010-03-19 10:09:59 -0700 | [diff] [blame] | 1273 | } |
Romain Guy | 4f43ae0 | 2010-03-17 10:31:15 -0700 | [diff] [blame] | 1274 | } |
| 1275 | }); |
Romain Guy | 6a67810 | 2010-03-16 13:49:31 -0700 | [diff] [blame] | 1276 | } |
| 1277 | } |
Romain Guy | 6a67810 | 2010-03-16 13:49:31 -0700 | [diff] [blame] | 1278 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1279 | } |