I have a MultiAutoCompleteTextView
. It works fine. But I want to show suggestion dropdown only when user type @ on it (like tagging user in facebook app). I have no idea how to do it. Here is my code :
mChatbox = (MultiAutoCompleteTextView) findViewById(R.id.chatbox);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, userList);
mChatBox.setAdapter(adapter);
mChatBox.setTokenizer(new SpaceTokenizer());
public class SpaceTokenizer implements MultiAutoCompleteTextView.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) == ' ') {
i++;
}
return i;
}
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == ' ') {
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 + " ");
TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
Object.class, sp, 0);
return sp;
} else {
return text + " ";
}
}
}
If you want to detect your string starts with '@' for mention (tag) someone or '#' for hashTag, then do query or filter with it, you could follow this code belows:
It references from the link Multiautocompletetextview, Show autocomplete drop down only when user presses a key after '@' key (like mention in FB app) please scroll down to find @Phuong Sala answer.
Create a custom Text view extending MultiAutoCompleteTextView -> override enoughToFilter() -> set the threshold to 0 (the bold variable in the below given code) :
}
Using this code you'll see the auto suggested list on press of
@
I got a solution by myself. I create custom view which extends
MultiAutoCompleteTextView
and overrideperformFiltering
in it. Check if first char is "@", then filter the next chars after it. Otherwise, replace chars with "*" to avoid filtering. Here is my code.