AutoCompleteTextView detect when selected entry from list edited by user
Asked Answered
S

3

18

I have an AutoCompleteTextView I use to select an item from a long list. The user should only be able to select a predetermined item from the list. They should not be able to enter their own item.

The way I check to make sure they submit only an item from the list is to use setOnItemClickListener to trigger a boolean flag. The problem is that after the boolean flag is set to true, they can still edit the selected text of the item. I need to detect this and set the boolean flag to false again. How do I do this. I have seen a suggestion to use onKeyDown, but I am not sure how to implement this.

Seavey answered 12/3, 2013 at 8:15 Comment(1)
Refer the below link #7056034Detribalize
C
56

You can add text changed listener:

autoCompleteTextView.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {                

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});
Confutation answered 12/3, 2013 at 8:22 Comment(3)
How do you distinguish dropdown selection from replacing the whole string by the user with this solution ?Verdellverderer
I did this by searching the adapter for new string. My use case however is hooked up to an api and shows search results. So the issue for me was that when the text got updated after selecting a search result in the popup, the popup would appear again instead of disapear.Lunge
@Jesson Atherton How did you sole this issue. Its like infinte loop for meCunningham
P
2

Implement a TextWatcher, which will give you 3 methods which will constantly get call backs when someone changes the text. If the string grows, your user is typing by himself again.

Plectrum answered 12/3, 2013 at 8:18 Comment(0)
V
0

Use

AutoCompleteTextView#setOnItemSelectedListener() 

- works like a charm.

Verdellverderer answered 6/2, 2017 at 21:53 Comment(1)
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Mudd

© 2022 - 2024 — McMap. All rights reserved.