AutoCompleteTextView provides suggestion which would replace the entire content of the view. Is there a View
class which provides this facility at word level? For example if I have "All work and no pl", then I should be able to suggest "play", "please", etc. which would complete that word. If I select "play" it should become "All work and no play ".

- 1,606
- 2
- 21
- 39
2 Answers
It is possible using MultiAutoCompleteTextView and a custom Tokenizer
I have repurposed the CommaTokenizer from Android's source code so that any character can be used to split up the words in MultiAutoCompleteTextView. Beware, untested code
public static class CustomTokenizer implements MultiAutoCompleteTextView.Tokenizer {
private final char WHITE_SPACE = ' ';
private final char TOKEN_TERMINATING_CHAR;
private final String TOKEN_TERMINATING_STRING;
public CustomTokenizer(char tokenTerminatingChar) {
TOKEN_TERMINATING_CHAR = tokenTerminatingChar;
if (TOKEN_TERMINATING_CHAR == WHITE_SPACE) {
TOKEN_TERMINATING_STRING = String.valueOf(TOKEN_TERMINATING_CHAR);
} else {
TOKEN_TERMINATING_STRING = String.valueOf(TOKEN_TERMINATING_CHAR) + String.valueOf(WHITE_SPACE);
}
}
@Override
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != TOKEN_TERMINATING_CHAR) {
i--;
}
while (i < cursor && text.charAt(i) == WHITE_SPACE) {
i++;
}
return i;
}
@Override
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == TOKEN_TERMINATING_CHAR) {
return i;
} else {
i++;
}
}
return len;
}
@Override
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == WHITE_SPACE) {
i--;
}
if (i > 0 && text.charAt(i - 1) == TOKEN_TERMINATING_CHAR) {
return text;
} else {
if (text instanceof Spanned) {
SpannableString sp = new SpannableString(text + TOKEN_TERMINATING_STRING);
TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
Object.class, sp, 0);
return sp;
} else {
return text + TOKEN_TERMINATING_STRING;
}
}
}
}

- 1,606
- 2
- 21
- 39
I think you can do it by custom an Adapter which implements Filterable, by overriding method getFilter() you can return a custom Filter.
In the custom Filter, a method named performFiltering(CharSequence prefix) will be overridden. the parameter prefix would be "All work and no pl " in your example, you can parse it and get "pl", then it is time to get match words and return them.
In another overridden method publishResults(CharSequence constraint, FilterResults results) you can get suggest result and do something to refresh UI, Like notifyDataSetChanged() to refresh ListView.
At last, you can added "All work and no" before the "play" in OnItemClickListener of AutoCompleteTextView when user has clicked.
Here is code of how to custom adapter and filter in Mithun's answer:
Android AutoCompleteTextView with Custom Adapter filtering not working
-
Thanks for the suggestion Jared. I thought I overlooked a tailor made view for this!! – x-treme Sep 09 '14 at 06:26