6

I have an AutocompleteTextView on my screen. By default, when user clicks on an item of an autocompletetextviews's dropdown item, it sets the text of this autocompletetextview to this chosen item value.

Is there any way to disable this? So when user clicks on a dropdown item, only onItemClickListener triggered?

Setting value to "" in onItemClickListener is not an option.

autoCompleteTextView.setOnItemClickListener { adapterView, view, i, l -> 
    autoCompleteTextView.setText("")
}

As I need my TextWatcher to not be triggered

Dannie
  • 115
  • 2
  • 11
  • Why? The whole point of an AutoCompleteTextView is to autocomplete what the user chooses. In any case, just unset your TextWatcher, set the text and then reset your TextWatcher. – TheWanderer Oct 22 '18 at 14:40
  • @TheWanderer and how I can know when to unset the textwatcher? I still need to monitor user input. – Dannie Oct 22 '18 at 14:48
  • Unset it when your item click listener is triggered, then reset it after you set the text back to blank inside that listener. – TheWanderer Oct 22 '18 at 14:49
  • @TheWanderer That's not how it works 1. user clicks on item 2. autocomplete set's it's value to chosen item 3. textwatcher triggered 4. onItemClickListener triggered 5. ... – Dannie Oct 22 '18 at 14:57
  • you dont need ANY `TextWatcher` - all you have to do is to implement a `Filter` in your `Filterable` adapter which you are passing to `AutoCompleteTextView` - see [here](https://stackoverflow.com/a/19860624/2252830) on how you can do that indirectly (not implementing `Filter` from the scratch) – pskink Oct 22 '18 at 15:09

1 Answers1

9

AutoCompleteTextView uses this class to detect clicks on its dropdown:

private class DropDownItemClickListener implements AdapterView.OnItemClickListener {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        performCompletion(v, position, id);
    }
}

Inside that performCompletion() method, there is this call to actually change the contents of the TextView:

replaceText(convertSelectionToString(selectedItem));

This replaceText() method is protected, which means you can create a subclass of AutoCompleteTextView and override it to do nothing:

public class MyAutoCompleteTextView extends AppCompatAutoCompleteTextView {

    public MyAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void replaceText(CharSequence text) {
        // do nothing
    }
}

Now just replace your <AutoCompleteTextView> tags with <com.example.yourprojecthere.MyAutoCompleteTextView> tags and you should be all set.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • 1
    Wow! Thanks. Working great. Sometimes I'm wondering how do some people know such details =) ? Did you encounter the same problem once? – Dannie Oct 22 '18 at 15:38
  • 1
    In this case, I had no idea before reading your question. I looked through the source code for `AutoCompleteTextView` and was able to find the relevant code in a few minutes. Some things jump out, like this line in the constructor: `mPopup.setOnItemClickListener(new DropDownItemClickListener());` – Ben P. Oct 22 '18 at 15:46
  • @Dannie so what do you need that `TextWatcher` for? why do you want to "watch" entered text? for filtering suggestions purposes? if so, use a `Filter` in your `Filterable` adapter, do not use those workarounds that come from using `TextWatcher` – pskink Oct 22 '18 at 16:00
  • @pskink I need it to get entered text to query the server, while the autocomplete just filtering what I have right now. So basically I have a new call to the server, when user inuts data (with debouncer of course). But when user selects an item from the dropdown I don't want to make one last unnececcary call, since the textwatcher is triggered. I still love it that way. TextWatcher is handels new input, AutoComplete handles filtering of what data I have – Dannie Oct 23 '18 at 08:49