Disable Android AutoCompleteTextView after user selects item from drop down

21,871

Solution 1

Answering my own question after a couple hours of hacking at this: It turns out you should implement your own OnItemClickListener and instead rely on the existing click listener to populate the TextView. I had originally implemented the onItemClickListener because it was using the results of Cursor.toString() to populate the text view. To change the output String, you should implement convertToString(Cursor) in your CursorAdapter. The CharSequence that gets returned will be populated in the text view.

Doing this will also prevent the dropdown from showing up again (since setText() triggers the completion behavior but the default onItemClickListener does not).

Solution 2

You can use the dismissDropDown() method of the AutoCompleteTextView object. Take a look at the documentation.

Solution 3

When we click on item suggested in AutoCompleteTextView.onTextChanged() is performed before onItemClick So, to avoid this try below code..

autocompletetextview.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (autocompletetextview.isPerformingCompletion()) {
            // An item has been selected from the list. Ignore.
        } else {
            // Perform your task here... Like calling web service, Reading data from SQLite database, etc...
        }
    }

    @Override
    public void afterTextChanged(final Editable editable) {

    }
});

Solution 4

If you wish to dissmis AutoCompleteTextView's dropdown you should use its post(Runnable r) method. It works for me :)

Here is an example:

mAutoCompleteTextView.post(new Runnable() {
    public void run() {
        mAutoCompleteTextView.dismissDropDown();
    }
}
Share:
21,871
magneticMonster
Author by

magneticMonster

Updated on July 09, 2022

Comments

  • magneticMonster
    magneticMonster almost 2 years

    I'm using Android's AutoCompleteTextView with a CursorAdapter to add autocomplete to an app. In the view's onItemClickListener() (i.e. when the user touches one of the autocompleted drop down items) I retrieve the text and place it in the EditText so that the user can modify it if they need to.

    However, when I call setText() on the TextView the autocomplete behavior is triggered and the dropdown shows again. I'd like to only show the dropdown if the user types new text with the keyboard. Is there a way to do this?

  • magneticMonster
    magneticMonster over 12 years
    I want to prevent the drop down from showing until new keys are typed. If I use dismissDropDown() the dropdown will briefly show before getting dismissed.
  • Franziskus Karsunke
    Franziskus Karsunke over 12 years
    If you put the method call in the onItemClickListener() it should dismiss the list, when you click on one item. Isn't this what you want?
  • BPDESILVA
    BPDESILVA about 10 years
    I also added the same snippet in onConfigurationChanged in my activity
  • Vucko
    Vucko almost 6 years
    This didn't work for me, the dropdown just appeared again.
  • CopsOnRoad
    CopsOnRoad almost 6 years
    @Vucko Please check my answer. This trick wasn't working for me also.
  • Vucko
    Vucko almost 6 years
    Yeah I tried this also but how do I know intValue? It's different with each search mate :)
  • CopsOnRoad
    CopsOnRoad almost 6 years
    In my case, I was using a hard coded value. This is like a max value and if you have less items for a particular choice, the height will reduce to make up with your new choices. So, there is like no disadvantage of using a hard coded value.
  • Someone Somewhere
    Someone Somewhere over 5 years
    YES! this is exactly what I needed. You were right that my app made an API call after a user selected an item in the list, which then re-populated the list and showed it ! That's now fixed, thanks!
  • Cătălin Florescu
    Cătălin Florescu over 5 years
    Thanks, autocompletetextview.isPerformingCompletion() just saved me.
  • Smitty-Werben-Jager-Manjenson
    Smitty-Werben-Jager-Manjenson almost 5 years
    @Vucko something is likely putting the focus, and thus the cursor back into the autoCompleteTextBox, causing the list to reappear.
  • Vucko
    Vucko almost 5 years
    I did solve it in the meanwhile. Don't remember how now though :D
  • Vadim Kotov
    Vadim Kotov almost 5 years
    What if my adapter extends BaseAdapter, not CursorAdapter? I do not have such method, but I have the same issue (I override getFilter method).
  • Vadim Kotov
    Vadim Kotov almost 5 years
    Ah, found it: you need to override Filter.convertResultToString to provide String representation.
  • Rafay Ali
    Rafay Ali over 4 years
    The best and easiest solution that worked for me. Thanks!