Methods Summary |
---|
private int | buildDropDown()Builds the popup window's content and returns the height the popup
should have. Returns -1 when the content already exists.
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; i < N; i++) {
Object item = mAdapter.getItem(i);
long id = mAdapter.getItemId(i);
completions[i] = new CompletionInfo(id, i,
convertSelectionToString(item));
}
imm.displayCompletions(this, completions);
}
}
if (mDropDownList == null) {
Context context = getContext();
mHideSelector = new ListSelectorHider();
mDropDownList = new DropDownListView(context);
mDropDownList.setSelector(mDropDownListHighlight);
mDropDownList.setAdapter(mAdapter);
mDropDownList.setVerticalFadingEdgeEnabled(true);
mDropDownList.setOnItemClickListener(mDropDownItemClickListener);
mDropDownList.setFocusable(true);
mDropDownList.setFocusableInTouchMode(true);
if (mItemSelectedListener != null) {
mDropDownList.setOnItemSelectedListener(mItemSelectedListener);
}
dropDownView = mDropDownList;
View hintView = getHintView(context);
if (hintView != null) {
// if an hint has been specified, we accomodate more space for it and
// add a text view in the drop down menu, at the bottom of the list
LinearLayout hintContainer = new LinearLayout(context);
hintContainer.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 0, 1.0f
);
hintContainer.addView(dropDownView, hintParams);
hintContainer.addView(hintView);
// measure the hint's height to find how much more vertical space
// we need to add to the drop down's height
int widthSpec = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST);
int heightSpec = MeasureSpec.UNSPECIFIED;
hintView.measure(widthSpec, heightSpec);
hintParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
otherHeights = hintView.getMeasuredHeight() + hintParams.topMargin
+ hintParams.bottomMargin;
dropDownView = hintContainer;
}
mPopup.setContentView(dropDownView);
} else {
dropDownView = (ViewGroup) mPopup.getContentView();
final View view = dropDownView.findViewById(HINT_VIEW_ID);
if (view != null) {
LinearLayout.LayoutParams hintParams =
(LinearLayout.LayoutParams) view.getLayoutParams();
otherHeights = view.getMeasuredHeight() + hintParams.topMargin
+ hintParams.bottomMargin;
}
}
// Max height available on the screen for a popup anchored to us
final int maxHeight = mPopup.getMaxAvailableHeight(this, mDropDownVerticalOffset);
//otherHeights += dropDownView.getPaddingTop() + dropDownView.getPaddingBottom();
return mDropDownList.measureHeightOfChildren(MeasureSpec.UNSPECIFIED,
0, ListView.NO_POSITION, maxHeight - otherHeights, 2) + otherHeights;
|
public void | clearListSelection()Clear the list selection. This may only be temporary, as user input will often bring
it back.
if (mDropDownList != null) {
mDropDownList.hideSelector();
mDropDownList.requestLayout();
}
|
protected java.lang.CharSequence | convertSelectionToString(java.lang.Object selectedItem)Converts the selected item from the drop down list into a sequence
of character that can be used in the edit box.
return mFilter.convertResultToString(selectedItem);
|
public void | dismissDropDown()Closes the drop down if present on screen.
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) {
imm.displayCompletions(this, null);
}
mPopup.dismiss();
mPopup.setContentView(null);
mDropDownList = null;
|
void | doAfterTextChanged()
if (mBlockCompletion) return;
// 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);
}
}
|
void | doBeforeTextChanged()
if (mBlockCompletion) return;
// 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);
|
public boolean | enoughToFilter()Returns true 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.
if (DEBUG) Log.v(TAG, "Enough to filter: len=" + getText().length()
+ " threshold=" + mThreshold);
return getText().length() >= mThreshold;
|
void | finishInit()Sets this to be single line; a separate method so
MultiAutoCompleteTextView can skip this.
setSingleLine();
|
public ListAdapter | getAdapter()Returns a filterable list adapter used for auto completion.
return mAdapter;
|
public int | getDropDownAnchor()Returns the id for the view that the auto-complete drop down list is anchored to.
return mDropDownAnchorId;
|
private android.view.View | getDropDownAnchorView()Used for lazy instantiation of the anchor view from the id we have. If the value of
the id is NO_ID or we can't find a view for the given id, we return this TextView as
the default anchoring point.
if (mDropDownAnchorView == null && mDropDownAnchorId != View.NO_ID) {
mDropDownAnchorView = getRootView().findViewById(mDropDownAnchorId);
}
return mDropDownAnchorView == null ? this : mDropDownAnchorView;
|
public int | getDropDownWidth()Returns the current width for the auto-complete drop down list. This can
be a fixed width, or {@link ViewGroup.LayoutParams#FILL_PARENT} to fill the screen, or
{@link ViewGroup.LayoutParams#WRAP_CONTENT} to fit the width of its anchor view.
return mDropDownWidth;
|
protected Filter | getFilter()Returns the Filter obtained from {@link Filterable#getFilter},
or null if {@link #setAdapter} was not called with
a Filterable.
return mFilter;
|
private android.view.View | getHintView(android.content.Context context)
if (mHintText != null && mHintText.length() > 0) {
final TextView hintView = (TextView) LayoutInflater.from(context).inflate(
mHintResource, null).findViewById(com.android.internal.R.id.text1);
hintView.setText(mHintText);
hintView.setId(HINT_VIEW_ID);
return hintView;
} else {
return null;
}
|
public AdapterView.OnItemClickListener | getItemClickListener()Returns the listener that is notified whenever the user clicks an item
in the drop down list.
return mItemClickListener;
|
public AdapterView.OnItemSelectedListener | getItemSelectedListener()Returns the listener that is notified whenever the user selects an
item in the drop down list.
return mItemSelectedListener;
|
public int | getListSelection()Get the position of the dropdown view selection, if there is one. Returns
{@link ListView#INVALID_POSITION ListView.INVALID_POSITION} if there is no dropdown or if
there is no selection.
if (mPopup.isShowing() && (mDropDownList != null)) {
return mDropDownList.getSelectedItemPosition();
}
return ListView.INVALID_POSITION;
|
public AdapterView.OnItemClickListener | getOnItemClickListener()Returns the listener that is notified whenever the user clicks an item
in the drop down list.
return mItemClickListener;
|
public AdapterView.OnItemSelectedListener | getOnItemSelectedListener()Returns the listener that is notified whenever the user selects an
item in the drop down list.
return mItemSelectedListener;
|
public int | getThreshold()Returns the number of characters the user must type before the drop
down list is shown.
return mThreshold;
|
public android.widget.AutoCompleteTextView$Validator | getValidator()Returns the Validator set with {@link #setValidator},
or null if it was not set.
return mValidator;
|
public boolean | isPerformingCompletion()Identifies whether the view is currently performing a text completion, so subclasses
can decide whether to respond to text changed events.
return mBlockCompletion;
|
public boolean | isPopupShowing()Indicates whether the popup menu is showing.
return mPopup.isShowing();
|
protected void | onAttachedToWindow()
super.onAttachedToWindow();
mAttachCount++;
|
public void | onCommitCompletion(android.view.inputmethod.CompletionInfo completion)
if (isPopupShowing()) {
mBlockCompletion = true;
replaceText(completion.getText());
mBlockCompletion = false;
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());
}
}
|
protected void | onDetachedFromWindow()
dismissDropDown();
mAttachCount--;
super.onDetachedFromWindow();
|
public void | onFilterComplete(int count)
if (mAttachCount <= 0) return;
/*
* 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();
}
|
protected void | onFocusChanged(boolean focused, int direction, android.graphics.Rect previouslyFocusedRect)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
performValidation();
if (!focused) {
dismissDropDown();
}
|
public boolean | onKeyDown(int keyCode, android.view.KeyEvent event)
// when the drop down is shown, we drive it directly
if (isPopupShowing()) {
// the key events are forwarded to the list in the drop down view
// note that ListView handles space but we don't want that to happen
// also if selection is not currently in the drop down, then don't
// let center or enter presses go there since that would cause it
// to select one of its items
if (keyCode != KeyEvent.KEYCODE_SPACE
&& (mDropDownList.getSelectedItemPosition() >= 0
|| (keyCode != KeyEvent.KEYCODE_ENTER
&& keyCode != KeyEvent.KEYCODE_DPAD_CENTER))) {
int curIndex = mDropDownList.getSelectedItemPosition();
boolean consumed;
final boolean below = !mPopup.isAboveAnchor();
if ((below && keyCode == KeyEvent.KEYCODE_DPAD_UP && curIndex <= 0) ||
(!below && keyCode == KeyEvent.KEYCODE_DPAD_DOWN && curIndex >=
mDropDownList.getAdapter().getCount() - 1)) {
// When the selection is at the top, we block the key
// event to prevent focus from moving.
mDropDownList.hideSelector();
mDropDownList.requestLayout();
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
mPopup.update();
return true;
}
consumed = mDropDownList.onKeyDown(keyCode, event);
if (DEBUG) Log.v(TAG, "Key down: code=" + keyCode + " list consumed="
+ consumed);
if (consumed) {
// If it handled the key event, then the user is
// navigating in the list, so we should put it in front.
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
// Here's a little trick we need to do to make sure that
// the list view is actually showing its focus indicator,
// by ensuring it has focus and getting its window out
// of touch mode.
mDropDownList.requestFocusFromTouch();
mPopup.update();
switch (keyCode) {
// avoid passing the focus from the text view to the
// next component
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_DPAD_UP:
return true;
}
} else {
if (below && keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
// when the selection is at the bottom, we block the
// event to avoid going to the next focusable widget
Adapter adapter = mDropDownList.getAdapter();
if (adapter != null && curIndex == adapter.getCount() - 1) {
return true;
}
} else if (!below && keyCode == KeyEvent.KEYCODE_DPAD_UP && curIndex == 0) {
return true;
}
}
}
} else {
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_DOWN:
performValidation();
}
}
mLastKeyCode = keyCode;
boolean handled = super.onKeyDown(keyCode, event);
mLastKeyCode = KeyEvent.KEYCODE_UNKNOWN;
if (handled && isPopupShowing() && mDropDownList != null) {
clearListSelection();
}
return handled;
|
public boolean | onKeyPreIme(int keyCode, android.view.KeyEvent event)
if (isPopupShowing()) {
// special case for the back key, we do not even try to send it
// to the drop down list but instead, consume it immediately
if (keyCode == KeyEvent.KEYCODE_BACK) {
dismissDropDown();
return true;
}
}
return super.onKeyPreIme(keyCode, event);
|
public boolean | onKeyUp(int keyCode, android.view.KeyEvent event)
if (isPopupShowing() && mDropDownList.getSelectedItemPosition() >= 0) {
boolean consumed = mDropDownList.onKeyUp(keyCode, event);
if (consumed) {
switch (keyCode) {
// if the list accepts the key events and the key event
// was a click, the text view gets the selected item
// from the drop down as its content
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_DPAD_CENTER:
performCompletion();
return true;
}
}
}
return super.onKeyUp(keyCode, event);
|
public void | onWindowFocusChanged(boolean hasWindowFocus)
super.onWindowFocusChanged(hasWindowFocus);
performValidation();
if (!hasWindowFocus) {
dismissDropDown();
}
|
public void | performCompletion()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.
performCompletion(null, -1, -1);
|
private void | performCompletion(android.view.View selectedView, int position, long id)
if (isPopupShowing()) {
Object selectedItem;
if (position < 0) {
selectedItem = mDropDownList.getSelectedItem();
} else {
selectedItem = mAdapter.getItem(position);
}
if (selectedItem == null) {
Log.w(TAG, "performCompletion: no selected item");
return;
}
mBlockCompletion = true;
replaceText(convertSelectionToString(selectedItem));
mBlockCompletion = false;
if (mItemClickListener != null) {
final DropDownListView list = mDropDownList;
if (selectedView == null || position < 0) {
selectedView = list.getSelectedView();
position = list.getSelectedItemPosition();
id = list.getSelectedItemId();
}
mItemClickListener.onItemClick(list, selectedView, position, id);
}
}
dismissDropDown();
|
protected void | performFiltering(java.lang.CharSequence text, int keyCode)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 .
mFilter.filter(text, this);
|
public void | performValidation()If a validator was set on this view and the current string is not valid,
ask the validator to fix it.
if (mValidator == null) return;
CharSequence text = getText();
if (!TextUtils.isEmpty(text) && !mValidator.isValid(text)) {
setText(mValidator.fixText(text));
}
|
protected void | replaceText(java.lang.CharSequence text)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.
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 | resetListAndClearViews()We're changing the adapter and its views so really, really clear everything out
if (mDropDownList != null) {
mDropDownList.resetListAndClearViews();
}
|
public void | setAdapter(T adapter)Changes the list of data used for auto completion. The provided list
must be a filterable list adapter.
The caller is still responsible for managing any resources used by the adapter.
Notably, when the AutoCompleteTextView is closed or released, the adapter is not notified.
A common case is the use of {@link android.widget.CursorAdapter}, which
contains a {@link android.database.Cursor} that must be closed. This can be done
automatically (see
{@link android.app.Activity#startManagingCursor(android.database.Cursor)
startManagingCursor()}),
or by manually closing the cursor when the AutoCompleteTextView is dismissed.
mAdapter = adapter;
if (mAdapter != null) {
//noinspection unchecked
mFilter = ((Filterable) mAdapter).getFilter();
} else {
mFilter = null;
}
if (mDropDownList != null) {
mDropDownList.setAdapter(mAdapter);
}
|
public void | setCompletionHint(java.lang.CharSequence hint)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.
mHintText = hint;
|
public void | setDropDownAnchor(int id)Sets the view to which the auto-complete drop down list should anchor. The view
corresponding to this id will not be loaded until the next time it is needed to avoid
loading a view which is not yet instantiated.
mDropDownAnchorId = id;
mDropDownAnchorView = null;
|
public void | setDropDownHorizontalOffset(int horizontalOffset)Set the horizontal offset with respect to {@link #setDropDownAnchor(int)}
mDropDownHorizontalOffset = horizontalOffset;
|
public void | setDropDownVerticalOffset(int verticalOffset)Set the vertical offset with respect to {@link #setDropDownAnchor(int)}
mDropDownVerticalOffset = verticalOffset;
|
public void | setDropDownWidth(int width)Sets the current width for the auto-complete drop down list. This can
be a fixed width, or {@link ViewGroup.LayoutParams#FILL_PARENT} to fill the screen, or
{@link ViewGroup.LayoutParams#WRAP_CONTENT} to fit the width of its anchor view.
mDropDownWidth = width;
|
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, r - l, -1);
}
return result;
|
public void | setListSelection(int position)Set the position of the dropdown view selection.
if (mPopup.isShowing() && (mDropDownList != null)) {
mDropDownList.setSelection(position);
// ListView.setSelection() will call requestLayout()
}
|
public void | setOnItemClickListener(AdapterView.OnItemClickListener l)Sets the listener that will be notified when the user clicks an item
in the drop down list.
mItemClickListener = l;
|
public void | setOnItemSelectedListener(AdapterView.OnItemSelectedListener l)Sets the listener that will be notified when the user selects an item
in the drop down list.
mItemSelectedListener = l;
|
public void | setThreshold(int threshold)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.
if (threshold <= 0) {
threshold = 1;
}
mThreshold = threshold;
|
public void | setValidator(android.widget.AutoCompleteTextView$Validator validator)Sets the validator used to perform text validation.
mValidator = validator;
|
public void | showDropDown()Displays the drop down on screen.
int height = buildDropDown();
if (mPopup.isShowing()) {
int widthSpec;
if (mDropDownWidth == ViewGroup.LayoutParams.FILL_PARENT) {
// The call to PopupWindow's update method below can accept -1 for any
// value you do not want to update.
widthSpec = -1;
} else if (mDropDownWidth == ViewGroup.LayoutParams.WRAP_CONTENT) {
widthSpec = getDropDownAnchorView().getWidth();
} else {
widthSpec = mDropDownWidth;
}
mPopup.update(getDropDownAnchorView(), mDropDownHorizontalOffset,
mDropDownVerticalOffset, widthSpec, height);
} else {
if (mDropDownWidth == ViewGroup.LayoutParams.FILL_PARENT) {
mPopup.setWindowLayoutMode(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
} else {
mPopup.setWindowLayoutMode(0, ViewGroup.LayoutParams.WRAP_CONTENT);
if (mDropDownWidth == ViewGroup.LayoutParams.WRAP_CONTENT) {
mPopup.setWidth(getDropDownAnchorView().getWidth());
} else {
mPopup.setWidth(mDropDownWidth);
}
}
mPopup.setHeight(height);
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
mPopup.setOutsideTouchable(true);
mPopup.setTouchInterceptor(new PopupTouchIntercepter());
mPopup.showAsDropDown(getDropDownAnchorView(),
mDropDownHorizontalOffset, mDropDownVerticalOffset);
mDropDownList.setSelection(ListView.INVALID_POSITION);
mDropDownList.hideSelector();
mDropDownList.requestFocus();
post(mHideSelector);
}
|