Development issue/problem:
I want my EditText to work like an AutoComplete, so I write in an 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 have a simple EditText and a button called Search. So, if we type a place name in EditText and click the Search button, it means it should go to that place on the map.
So I want EditText to work like AutoComplete.
How can I do that?
How can I solve this problem?
Solution 1:
Just use AutoCompleteTextView instead of the normal EditText.
The Hi-Autocomplete function will be useful.
EDIT: The above link seems to have expired. The new site is here: https://developer.android.com/training/keyboard-input/style#AutoComplete.
Solution 2:
First convert your EditText->AutoCompleteTextView
Next, connect your XML file to AutoCompleteTextView via the ArrayAdapter.
Assuming that the array of XML strings you created is named list_of_countries, you can link it to your AutoCompleteTextView as follows:
String[] countries = getResources().getStringArray(R.array.list_of_countries) ;
ArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,countries) ;
actv.setAdapter(adapter) ;
Solution 3:
I use this code:
1) In AndroidManifest.xml
2) For xml layouts, use AutoCompleteTextView instead of EditText.
3) Use it in the activity file
private ArrayAdapter getEmailAddressAdapter(Context context) [
Account[] accounts = AccountManager.get(context).getAccounts() ;
String[] addresses = new String [accounts.length] ;
for (int i = 0 ; i (context, android.R.layout.simple_dropdown_item_1line, addresses) ;
}
4) For creative activities :
AutoCompleteTextView autoCompleteTextView1 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1) ;
autoCompleteTextView1.setAdapter(getEmailAddressAdapter(this) ) ;
Solution 4:
By default, the ArrayAdapter filters only on the first characters. If you also want to see the words that contain the keyword, you must use a custom ArrayAdapter and override the getView and getFilter methods. See the complete solution I provided in another StackOverflow question: https://stackoverflow.com/a/37298258/1808829
A piece of code:
The public class AutoSuggestAdapter extends ArrayAdapter
{
@overview
public view getView(int position, View convertView, ViewGroup parent)
{
// handle view here
}
@Override
public Filter getFilter()
{
// implement filtering here
}
}
Solution no. 5:
This code is used to change the settings of MultiAutoCompleteTextView
ArrayAdapter adapter = new ArrayAdapter(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 here is this code to separate words with a space and n characters…. (Why do we need this code? Because Normal multiAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); so, and it separates words into ‘,’ characters, but our code will help you to separate words into ‘ and ‘n’ characters).
/**
* This simple tokenizer can be used for lists of
* items separated by a comma and one or more spaces.
*/
public static class CommaTokenizer implements Tokenizer {
public int findTokenStart(charSequence text, cursor int) {
int i = cursor ;
while (i > 0 && text.charAt(i – 1) != ‘ ‘) {i–;} while (i 0 && text.charAt(i – 1) == ”) {i–;}
if (i > 0 && text.charAt(i – 1) == ‘ ‘) {
return text;
} else {
if (text instance of Spanned) {
SpannableString sp = new SpannableString(text + n);
TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
Object.class, sp, 0);
return sp;
} else {
return text +;
}
}
}
Good luck!
Related Tags:
multiautocompletetextview, auto text complete textview android, autocompletetextview android kotlin, android autocompletetextview dropdown style, autocompletetextview onitemselectedlistener, android autocompletetextview custom adapter, autocomplete edittext android github, android studio auto complete shortcut, turn on autocomplete android, android edittext suggestions, how to copy paste in android studio, how to copy text from edittext in android, autocompletetextview with api call android, android autocompletetextview stackoverflow, autocompletetextview with listview in android, autocompletetextview text change listener, autocompletetextview get selected item kotlin, edittext autofill hints, autocompletetextview not showing dropdown, autocompletetextview kotlin, android data binding autocompletetextview, kotlin autocomplete, autocompletetextview custom adapter kotlin, appcompatautocompletetextview adapter kotlin