Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: AutoCompleteTextView with default suggestions

How do I show some default suggestions for AutoCompleteTextView before the user type anything? I cannot find a way to do this even with creating a custom class that extends AutoCompleteTextView.

I want to show suggestions for common input values to save the user from typing.

Any suggestions?

like image 466
Kenny Ma Avatar asked Jul 07 '11 23:07

Kenny Ma


2 Answers

You should subclass AutoCompleteTextView and override enoughToFilter() to return true all the time. After that you can call performFiltering("",0) (it's a protected function, so you can export this call via a public function in your class).

Something like that:

public class ContactsAutoCompleteTextView extends AutoCompleteTextView {


    public ContactsAutoCompleteTextView(Context context) {
        super(context);
    }

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

    public ContactsAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean enoughToFilter() {
        return true;
    }

    public void temp() {
        performFiltering("",0);
    }
}
like image 147
Itay Kahana Avatar answered Nov 10 '22 17:11

Itay Kahana


Itay Kahana's answer is indeed correct. The only thing I would add is that instead of creating a temp() function, to override the onFocusChanged function. Personally I used the following:

 @Override
        protected void onFocusChanged (boolean focused, int direction, Rect previouslyFocusedRect) {
            if(focused)
                performFiltering("", 0);
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
        }
like image 2
Michel Avatar answered Nov 10 '22 16:11

Michel