Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 821
Dino Velić Avatar asked Mar 14 '23 01:03

Dino Velić


1 Answers

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

like image 137
Ravi Avatar answered Apr 02 '23 13:04

Ravi