Dino Velić
Dino Velić

Reputation: 898

AutoCompleteTextView - Show suggestions after selection

I am using AutoCompleteTextView for address suggestions.

What I want to do is when user types in the address (f.e. "Ma"), suggestions are shown like "Mary, Madley, Ma...".

Then, when user selects one of the suggestions he immediately gets another suggestions containing whole address.

For example: He selected "Mary" and he gets suggestions like "Mary 123, Boston", "Mary 1566, New York", "Mary Jane 569, New York".

The problem is that suggestions are filled in adapter, but not shown. The drop down list doesn't show after selection.

So far my text watcher is assigned to AutoCompleteTextView responsible for suggestions:

TextWatcher textWatcher = 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) {

        if(etStreet.isPerformingCompletion())
            return;

        List<String> arrayValues = getValues();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_dropdown_item_1line, arrayValues);
        etUlica.setAdapter(adapter);

    }
};

I've tried calling showDropDown() on item click, text change and every other event, but it just won't show up. It only shows when user types on keyboard.

Upvotes: 4

Views: 3699

Answers (1)

Ravi
Ravi

Reputation: 35559

write below code in your AutoCompleteTextView.setOnItemClickListener()

autoComplete.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            autoComplete.postDelayed(new Runnable() {
                @Override
                public void run() {
                    autoComplete.showDropDown();
                }
            },100);
            autoComplete.setText(autoComplete.getText().toString());
            autoComplete.setSelection(autoComplete.getText().length());

        }
    });

And that's it, it will work like charm!!!

This will give you hint for your question, change according to your need and adapter data

Upvotes: 7

Related Questions