45

I want my EditText should work as AutoComplete, for that I write in XML file

android:inputType="textAutoComplete|textAutoCorrect"

but it's not working.

I am working with API v2.2 and my Activity extends MapActivity, there I put a simple EditText and a button named "Search". so if we type the location name in EditText and press search button means it should go to that location in map. So I want that EditText to work as a AutoComplete. How can I do that?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Jyosna
  • 4,436
  • 13
  • 60
  • 93

5 Answers5

59

Just use an AutoCompleteTextView instead of normal EditText.

hello-autocomplete will be helpful.

EDIT: The above link looks like has expired. The new page is here: https://developer.android.com/training/keyboard-input/style#AutoComplete

user1506104
  • 6,554
  • 4
  • 71
  • 89
Arnab Chakraborty
  • 7,442
  • 9
  • 46
  • 69
  • Sory, I searched from google first i got AutoCompleteTextView also there. but I thought may be there any other to work on this. Thats why i asked. – Jyosna Jun 27 '11 at 06:07
  • @Jyosna - Why would you want another way? Have you tried the `AutoCompleteTextView`? Does it not solve your problem? – Arnab Chakraborty Jun 27 '11 at 06:36
  • I want like google search engine, like whatever i write there it should autocomplete. For example i did for location , I just entry some city name into an xml file and that AutoCompleteTextView work for only those city name what i mention in String array in string.xml. But I want my AutoCompleteTextview should work as google search box. How can I do that, Do have any idea? – Jyosna Jun 27 '11 at 06:42
  • I do not know how Google generates its auto completes exactly, but I believe it generates them on a statistical basis. What you could do is save the search queries to an external file every time the user searches something (or you could use some algorithm to generate better suggestions), and load the auto complete strings at runtime from that file. Just Google Android File Handling, I'm sure you'll find lots of tutorials. – Arnab Chakraborty Jun 27 '11 at 06:52
  • Here's a nice and easy tutorial http://android.foxykeep.com/dev/how-to-add-autocompletion-to-an-edittext It also covers autocomplete through a WebService. – Maragues Apr 09 '13 at 11:25
  • Can I use this for a multiline edittext with suggestions for comma separated tags? – cherry-wave May 09 '16 at 12:54
20

First convert your EditText->AutoCompleteTextView

Then link your XML file to the AutoCompleteTextView using a ArrayAdapter

Assume that the XML string-array you created is named as list_of_countries then it can be linked to your AutoCompleteTextView as follows:

String[] countries = getResources().getStringArray(R.array.list_of_countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,countries);
actv.setAdapter(adapter);
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Bestin John
  • 1,765
  • 1
  • 20
  • 23
14

I use this code:

enter image description here

1) On AndroidManifest.xml

<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>

2) On xml layout you must use AutoCompleteTextView instead of EditText.

<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:text="AutoCompleteTextView" />

3) Use this on Activity file

private ArrayAdapter<String> getEmailAddressAdapter(Context context) {
    Account[] accounts = AccountManager.get(context).getAccounts();
    String[] addresses = new String[accounts.length];
    for (int i = 0; i < accounts.length; i++) { 
        addresses[i] = accounts[i].name;
    }
    return new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, addresses);
}

4) On onCreate activity:

AutoCompleteTextView autoCompleteTextView1 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
autoCompleteTextView1.setAdapter(getEmailAddressAdapter(this));
Milad Ghiravani
  • 1,625
  • 23
  • 43
  • 2
    This only shows suggestions after the user starts typing. Also i'm not sure if it it safe with regards to the new permissions in android M. – Zapnologica Jan 08 '17 at 20:24
4

Default ArrayAdapter filters only by the first characters. In case you want to see also words which contain the searching keyword, you need to use a custom ArrayAdapter and override its getView and getFilter methods. Take a look at a complete solution I provided in another StackOverflow question: https://stackoverflow.com/a/37298258/1808829

Some code fragment:

public class AutoSuggestAdapter extends ArrayAdapter
{
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        // handle view here
    }

    @Override
    public Filter getFilter()
    {
       // implement filtering here
    }
}
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
0

This code for change settings of MultiAutoCompleteTextView

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,codeKeyWords);
MultiAutoCompleteTextView autoCompleteTextView1 = (MultiAutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
autoCompleteTextView1.setAdapter(adapter);
autoCompleteTextView1.setThreshold(1);
autoCompleteTextView1.setTokenizer(new this.CommaTokenizer());

And below that code for make spliting words by space char and \n charactes.. (Why we need this code? Because Normal multiAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); like that and it make spliting the words by ',' character, But our code help you to make that spliting by these characters ' ' and '\n' )

/**
         * This simple Tokenizer can be used for lists where the items are
         * separated by a comma and one or more spaces.
         */
    public static class CommaTokenizer implements Tokenizer {
        public int findTokenStart(CharSequence text, int cursor) {
            int i = cursor;

            while (i > 0 && text.charAt(i - 1) != ' ') {
                i--;
            }
            while (i < cursor && text.charAt(i) == '\n') {
                i++;
            }

            return i;
        }

        public int findTokenEnd(CharSequence text, int cursor) {
            int i = cursor;
            int len = text.length();

            while (i < len) {
                if (text.charAt(i) == '\n') {
                    return i;
                } else {
                    i++;
                }
            }

            return len;
        }

        public CharSequence terminateToken(CharSequence text) {
            int i = text.length();

            while (i > 0 && text.charAt(i - 1) == ' ') {
                i--;
            }

            if (i > 0 && text.charAt(i - 1) == ' ') {
                return text;
            } else {
                if (text instanceof Spanned) {
                    SpannableString sp = new SpannableString(text + "\n");
                    TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
                                            Object.class, sp, 0);
                    return sp;
                } else {
                    return text + " ";
                }
            }
        }
Kamran Gasimov
  • 1,445
  • 1
  • 14
  • 11