Reputation: 2465
I have implemented an autocomplete edit text and it does show suggestions, but i want to achieve a different functionality as well.
Right now, i have a String of countries
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
and i am calling it in my adapter. now when i start to type any country name, my edit text start showing me suggestions, but
what i want is:
Lets say i want to write France 10 times in the edit text so i want my autocomplete edit text to give me suggestion when ever i start typing france it and so on,
but my edit text give me suggestion for only the first word i enter. how can i achieve this, like how can my edit text give me suggestion for every text that i enter?
Upvotes: 0
Views: 2541
Reputation: 14520
Check the below code, it will work like if you press a space and type something, you will get the suggestion list:
Xml Layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<MultiAutoCompleteTextView
android:id="@+id/macTvInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Code :
public class MainActivity extends Activity {
private static final String[] COUNTRIES = new String[] { "Belgium",
"France", "Italy", "Germany", "Spain" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
MultiAutoCompleteTextView macTvInput = (MultiAutoCompleteTextView) findViewById(R.id.macTvInput);
macTvInput.setAdapter(adapter);
macTvInput.setTokenizer(new SpaceTokenizer());
}
private class SpaceTokenizer implements Tokenizer {
private final char delimiter = ' ';
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != delimiter) {
i--;
}
while (i < cursor && text.charAt(i) == delimiter) {
i++;
}
return i;
}
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == delimiter) {
return i;
} else {
i++;
}
}
return len;
}
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == delimiter) {
i--;
}
return text;
}
}
}
Upvotes: 4
Reputation: 4569
Use MultiAutoCompleteTextView instead of AutoCompleteTextView
public class CountriesActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete_7);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.edit);
textView.setAdapter(adapter);
textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
Sources from : http://developer.android.com/reference/android/widget/MultiAutoCompleteTextView.html
Upvotes: 0
Reputation: 616
Use MultiAutoCompleteTextView
in your Layout, and implement code like this, here i used to split by commas
String[] colors = getResources().getStringArray(R.array.AndroidDesk);
adapter1 = new ArrayAdapter<String>(this,R.layout.suggestion_textview,colors);
//autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.editText1);
// set adapter for the auto complete fields
// autoComplete.setAdapter(adapter);
multiAutoComplete.setAdapter(adapter1);
// specify the minimum type of characters before drop-down list is shown
//autoComplete.setThreshold(1);
multiAutoComplete.setThreshold(2);
// comma to separate the different colors
multiAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
Upvotes: 0