/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.widget; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.Selection; import android.text.TextUtils; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.EditorInfo; import com.android.internal.R; /** *
An editable text view that shows completion suggestions automatically * while the user is typing. The list of suggestions is displayed in a drop * down menu from which the user can choose an item to replace the content * of the edit box with.
* *The drop down can be dismissed at any time by pressing the back key or, * if no item is selected in the drop down, by pressing the enter/dpad center * key.
* *The list of suggestions is obtained from a data adapter and appears * only after a given number of characters defined by * {@link #getThreshold() the threshold}.
* *The following code snippet shows how to create a text view which suggests * various countries names while the user is typing:
* ** public class CountriesActivity extends Activity { * protected void onCreate(Bundle icicle) { * super.onCreate(icicle); * setContentView(R.layout.countries); * * ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, * android.R.layout.simple_dropdown_item_1line, COUNTRIES); * AutoCompleteTextView textView = (AutoCompleteTextView) * findViewById(R.id.countries_list); * textView.setAdapter(adapter); * } * * private static final String[] COUNTRIES = new String[] { * "Belgium", "France", "Italy", "Germany", "Spain" * }; * } ** * @attr ref android.R.styleable#AutoCompleteTextView_completionHint * @attr ref android.R.styleable#AutoCompleteTextView_completionThreshold * @attr ref android.R.styleable#AutoCompleteTextView_completionHintView * @attr ref android.R.styleable#AutoCompleteTextView_dropDownSelector */ public class AutoCompleteTextView extends EditText implements Filter.FilterListener { static final boolean DEBUG = false; static final String TAG = "AutoCompleteTextView"; private static final int HINT_VIEW_ID = 0x17; private CharSequence mHintText; private int mHintResource; private ListAdapter mAdapter; private Filter mFilter; private int mThreshold; private PopupWindow mPopup; private DropDownListView mDropDownList; private int mDropDownVerticalOffset; private int mDropDownHorizontalOffset; private Drawable mDropDownListHighlight; private AdapterView.OnItemClickListener mItemClickListener; private AdapterView.OnItemSelectedListener mItemSelectedListener; private final DropDownItemClickListener mDropDownItemClickListener = new DropDownItemClickListener(); private int mLastKeyCode = KeyEvent.KEYCODE_UNKNOWN; private boolean mOpenBefore; private Validator mValidator = null; private AutoCompleteTextView.ListSelectorHider mHideSelector; public AutoCompleteTextView(Context context) { this(context, null); } public AutoCompleteTextView(Context context, AttributeSet attrs) { this(context, attrs, com.android.internal.R.attr.autoCompleteTextViewStyle); } public AutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mPopup = new PopupWindow(context, attrs, com.android.internal.R.attr.autoCompleteTextViewStyle); TypedArray a = context.obtainStyledAttributes( attrs, com.android.internal.R.styleable.AutoCompleteTextView, defStyle, 0); mThreshold = a.getInt( R.styleable.AutoCompleteTextView_completionThreshold, 2); mHintText = a.getText(R.styleable.AutoCompleteTextView_completionHint); mDropDownListHighlight = a.getDrawable( R.styleable.AutoCompleteTextView_dropDownSelector); mDropDownVerticalOffset = (int) a.getDimension(R.styleable.AutoCompleteTextView_dropDownVerticalOffset, 0.0f); mDropDownHorizontalOffset = (int) a.getDimension(R.styleable.AutoCompleteTextView_dropDownHorizontalOffset, 0.0f); mHintResource = a.getResourceId(R.styleable.AutoCompleteTextView_completionHintView, R.layout.simple_dropdown_hint); // A little trickiness for backwards compatibility: if the app // didn't specify an explicit content type, then we will fill in the // auto complete flag for them. int contentType = a.getInt( R.styleable.AutoCompleteTextView_inputType, EditorInfo.TYPE_NULL); if (contentType == EditorInfo.TYPE_NULL) { contentType = getInputType(); if ((contentType&EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { contentType |= EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE; setRawInputType(contentType); } } a.recycle(); setFocusable(true); addTextChangedListener(new MyWatcher()); } /** * Sets this to be single line; a separate method so * MultiAutoCompleteTextView can skip this. */ /* package */ void finishInit() { setSingleLine(); } /** *
Sets the optional hint text that is displayed at the bottom of the * the matching list. This can be used as a cue to the user on how to * best use the list, or to provide extra information.
* * @param hint the text to be displayed to the user * * @attr ref android.R.styleable#AutoCompleteTextView_completionHint */ public void setCompletionHint(CharSequence hint) { mHintText = hint; } /** *Returns the number of characters the user must type before the drop * down list is shown.
* * @return the minimum number of characters to type to show the drop down * * @see #setThreshold(int) */ public int getThreshold() { return mThreshold; } /** *Specifies the minimum number of characters the user has to type in the * edit box before the drop down list is shown.
* *When threshold
is less than or equals 0, a threshold of
* 1 is applied.
Sets the listener that will be notified when the user clicks an item * in the drop down list.
* * @param l the item click listener */ public void setOnItemClickListener(AdapterView.OnItemClickListener l) { mItemClickListener = l; } /** *Sets the listener that will be notified when the user selects an item * in the drop down list.
* * @param l the item selected listener */ public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener l) { mItemSelectedListener = l; } /** *Returns the listener that is notified whenever the user clicks an item * in the drop down list.
* * @return the item click listener * * @deprecated Use {@link #getOnItemClickListener()} intead */ @Deprecated public AdapterView.OnItemClickListener getItemClickListener() { return mItemClickListener; } /** *Returns the listener that is notified whenever the user selects an * item in the drop down list.
* * @return the item selected listener * * @deprecated Use {@link #getOnItemSelectedListener()} intead */ @Deprecated public AdapterView.OnItemSelectedListener getItemSelectedListener() { return mItemSelectedListener; } /** *Returns the listener that is notified whenever the user clicks an item * in the drop down list.
* * @return the item click listener */ public AdapterView.OnItemClickListener getOnItemClickListener() { return mItemClickListener; } /** *Returns the listener that is notified whenever the user selects an * item in the drop down list.
* * @return the item selected listener */ public AdapterView.OnItemSelectedListener getOnItemSelectedListener() { return mItemSelectedListener; } /** *Returns a filterable list adapter used for auto completion.
* * @return a data adapter used for auto completion */ public ListAdapter getAdapter() { return mAdapter; } /** *Changes the list of data used for auto completion. The provided list * must be a filterable list adapter.
* * @param adapter the adapter holding the auto completion data * * @see #getAdapter() * @see android.widget.Filterable * @see android.widget.ListAdapter */ publictrue
if the amount of text in the field meets
* or exceeds the {@link #getThreshold} requirement. You can override
* this to impose a different standard for when filtering will be
* triggered.
*/
public boolean enoughToFilter() {
if (DEBUG) Log.v(TAG, "Enough to filter: len=" + getText().length()
+ " threshold=" + mThreshold);
return getText().length() >= mThreshold;
}
/**
* This is used to watch for edits to the text view. Note that we call
* to methods on the auto complete text view class so that we can access
* private vars without going through thunks.
*/
private class MyWatcher implements TextWatcher {
public void afterTextChanged(Editable s) {
doAfterTextChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
doBeforeTextChanged();
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
void doBeforeTextChanged() {
// when text is changed, inserted or deleted, we attempt to show
// the drop down
mOpenBefore = isPopupShowing();
if (DEBUG) Log.v(TAG, "before text changed: open=" + mOpenBefore);
}
void doAfterTextChanged() {
// if the list was open before the keystroke, but closed afterwards,
// then something in the keystroke processing (an input filter perhaps)
// called performCompletion() and we shouldn't do any more processing.
if (DEBUG) Log.v(TAG, "after text changed: openBefore=" + mOpenBefore
+ " open=" + isPopupShowing());
if (mOpenBefore && !isPopupShowing()) {
return;
}
// the drop down is shown only when a minimum number of characters
// was typed in the text view
if (enoughToFilter()) {
if (mFilter != null) {
performFiltering(getText(), mLastKeyCode);
}
} else {
// drop down is automatically dismissed when enough characters
// are deleted from the text view
dismissDropDown();
if (mFilter != null) {
mFilter.filter(null);
}
}
}
/**
* Indicates whether the popup menu is showing.
* * @return true if the popup menu is showing, false otherwise */ public boolean isPopupShowing() { return mPopup.isShowing(); } /** *Converts the selected item from the drop down list into a sequence * of character that can be used in the edit box.
* * @param selectedItem the item selected by the user for completion * * @return a sequence of characters representing the selected suggestion */ protected CharSequence convertSelectionToString(Object selectedItem) { return mFilter.convertResultToString(selectedItem); } /** *Starts filtering the content of the drop down list. The filtering
* pattern is the content of the edit box. Subclasses should override this
* method to filter with a different pattern, for instance a substring of
* text
.
Performs the text completion by converting the selected item from * the drop down list into a string, replacing the text box's content with * this string and finally dismissing the drop down menu.
*/ public void performCompletion() { performCompletion(null, -1, -1); } @Override public void onCommitCompletion(CompletionInfo completion) { if (isPopupShowing()) { replaceText(completion.getText()); if (mItemClickListener != null) { final DropDownListView list = mDropDownList; // Note that we don't have a View here, so we will need to // supply null. Hopefully no existing apps crash... mItemClickListener.onItemClick(list, null, completion.getPosition(), completion.getId()); } } } private void performCompletion(View selectedView, int position, long id) { if (isPopupShowing()) { Object selectedItem; if (position == -1) { selectedItem = mDropDownList.getSelectedItem(); } else { selectedItem = mAdapter.getItem(position); } replaceText(convertSelectionToString(selectedItem)); if (mItemClickListener != null) { final DropDownListView list = mDropDownList; if (selectedView == null || position == -1) { selectedView = list.getSelectedView(); position = list.getSelectedItemPosition(); id = list.getSelectedItemId(); } mItemClickListener.onItemClick(list, selectedView, position, id); } } dismissDropDown(); } /** *Performs the text completion by replacing the current text by the * selected item. Subclasses should override this method to avoid replacing * the whole content of the edit box.
* * @param text the selected suggestion in the drop down list */ protected void replaceText(CharSequence text) { setText(text); // make sure we keep the caret at the end of the text view Editable spannable = getText(); Selection.setSelection(spannable, spannable.length()); } public void onFilterComplete(int count) { /* * This checks enoughToFilter() again because filtering requests * are asynchronous, so the result may come back after enough text * has since been deleted to make it no longer appropriate * to filter. */ if (count > 0 && enoughToFilter()) { if (hasFocus() && hasWindowFocus()) { showDropDown(); } } else { dismissDropDown(); } } @Override public void onWindowFocusChanged(boolean hasWindowFocus) { super.onWindowFocusChanged(hasWindowFocus); performValidation(); if (!hasWindowFocus) { dismissDropDown(); } } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); performValidation(); if (!focused) { dismissDropDown(); } } @Override protected void onDetachedFromWindow() { dismissDropDown(); super.onDetachedFromWindow(); } /** *Closes the drop down if present on screen.
*/ public void dismissDropDown() { InputMethodManager imm = InputMethodManager.peekInstance(); if (imm != null) { imm.displayCompletions(this, null); } mPopup.dismiss(); mPopup.setContentView(null); mDropDownList = null; } @Override protected boolean setFrame(int l, int t, int r, int b) { boolean result = super.setFrame(l, t, r, b); if (mPopup.isShowing()) { mPopup.update(this, getMeasuredWidth() - mPaddingLeft - mPaddingRight, -1); } return result; } /** *Displays the drop down on screen.
*/ public void showDropDown() { int height = buildDropDown(); if (mPopup.isShowing()) { mPopup.update(this, mDropDownHorizontalOffset, mDropDownVerticalOffset, getMeasuredWidth() - mPaddingLeft - mPaddingRight, height); } else { mPopup.setWindowLayoutMode(0, ViewGroup.LayoutParams.WRAP_CONTENT); mPopup.setWidth(getMeasuredWidth() - mPaddingLeft - mPaddingRight); mPopup.setHeight(height); mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); mPopup.setOutsideTouchable(true); mPopup.setTouchInterceptor(new PopupTouchIntercepter()); mPopup.showAsDropDown(this, mDropDownHorizontalOffset, mDropDownVerticalOffset); mDropDownList.hideSelector(); mDropDownList.setSelection(0); mDropDownList.requestFocus(); post(mHideSelector); } } /** *Builds the popup window's content and returns the height the popup * should have. Returns -1 when the content already exists.
* * @return the content's height or -1 if content already exists */ private int buildDropDown() { ViewGroup dropDownView; int otherHeights = 0; if (mAdapter != null) { InputMethodManager imm = InputMethodManager.peekInstance(); if (imm != null) { int N = mAdapter.getCount(); if (N > 20) N = 20; CompletionInfo[] completions = new CompletionInfo[N]; for (int i=0; inull
if it was not set.
*
* @see #setValidator(android.widget.AutoCompleteTextView.Validator)
* @see #performValidation()
*/
public Validator getValidator() {
return mValidator;
}
/**
* If a validator was set on this view and the current string is not valid,
* ask the validator to fix it.
*
* @see #getValidator()
* @see #setValidator(android.widget.AutoCompleteTextView.Validator)
*/
public void performValidation() {
if (mValidator == null) return;
CharSequence text = getText();
if (!TextUtils.isEmpty(text) && !mValidator.isValid(text)) {
setText(mValidator.fixText(text));
}
}
/**
* Returns the Filter obtained from {@link Filterable#getFilter},
* or null
if {@link #setAdapter} was not called with
* a Filterable.
*/
protected Filter getFilter() {
return mFilter;
}
private class ListSelectorHider implements Runnable {
public void run() {
if (mDropDownList != null) {
mDropDownList.hideSelector();
}
}
}
private class PopupTouchIntercepter implements OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
mPopup.update();
}
return false;
}
}
private class DropDownItemClickListener implements AdapterView.OnItemClickListener {
public void onItemClick(AdapterView parent, View v, int position, long id) {
performCompletion(v, position, id);
}
}
/**
* Wrapper class for a ListView. This wrapper hijacks the focus to * make sure the list uses the appropriate drawables and states when * displayed on screen within a drop down. The focus is never actually * passed to the drop down; the list only looks focused.
*/ private static class DropDownListView extends ListView { /** *Creates a new list view wrapper.
* * @param context this view's context */ public DropDownListView(Context context) { super(context, null, com.android.internal.R.attr.dropDownListViewStyle); } /** *Avoids jarring scrolling effect by ensuring that list elements * made of a text view fit on a single line.
* * @param position the item index in the list to get a view for * @return the view for the specified item */ @Override protected View obtainView(int position) { View view = super.obtainView(position); if (view instanceof TextView) { ((TextView) view).setHorizontallyScrolling(true); } return view; } /** *Returns the top padding of the currently selected view.
* * @return the height of the top padding for the selection */ public int getSelectionPaddingTop() { return mSelectionTopPadding; } /** *Returns the bottom padding of the currently selected view.
* * @return the height of the bottom padding for the selection */ public int getSelectionPaddingBottom() { return mSelectionBottomPadding; } /** *Returns the focus state in the drop down.
* * @return true always */ @Override public boolean hasWindowFocus() { return true; } /** *Returns the focus state in the drop down.
* * @return true always */ @Override public boolean isFocused() { return true; } /** *Returns the focus state in the drop down.
* * @return true always */ @Override public boolean hasFocus() { return true; } protected int[] onCreateDrawableState(int extraSpace) { int[] res = super.onCreateDrawableState(extraSpace); if (false) { StringBuilder sb = new StringBuilder("Created drawable state: ["); for (int i=0; i