7

I have a requirement for implementing an edit text that the user can type in a anything but when they type in a new word that starts with '@' the autocomplete should start showing potential users.

I understand how to use the AutoCompleteTextView function for filtering. But I am not sure how to go about capturing the characters from the last word after the '@' symbol (ignoring any previous words).

Consequently when the user has been selected from the AutoCompleteTextView list, it should replace the the word with '@', eg.


"This is a message for @steve"


when the user clicks on "Steve" from the list, the text should replace to:


"This is a message for Steve"


I also need to obtain the string in a form that I can send off to the server. i.e. from the above example I would need to send off the string:


"This is a message for [username:steve@bloggs.com, id:44]."


I have looked at https://github.com/splitwise/TokenAutoComplete

Which seems great for typing emails in a list, but I am not sure how to cater it for my needs. Bare in mind, I need to support multiple/duplicate mentions:

eg


"This is a message for Steve and Bob. this is the second sentence in the message for Bob"


If anyone knows or has done anything like this, would really appreciate it!

FlashAsh80
  • 1,357
  • 1
  • 16
  • 27

2 Answers2

6

I ended up using the spyglass library from linkedin which does exactly what I was looking for. It provides a MentionsEditText (that can be customised). I also used a ListPopupWindow to show the suggestions in a list (like an AutoCompleteTextView).

Here is the link...

https://github.com/linkedin/Spyglass

FlashAsh80
  • 1,357
  • 1
  • 16
  • 27
4

The following method will extract words starting with "@":

private void parseText(String text) {
    String[] words = text.split("[ \\.]");
    for (int i = 0; i < words.length; i++) {
        if (words[i].length() > 0
                && words[i].charAt(0) == '@') {
            System.out.println(words[i]);
        }
    }
}

Once you have the words, use your auto complete filter and finally replace text using String.replace.

mapm
  • 1,315
  • 12
  • 14