Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AutoCompleteTextView with '@' mentions filtering like twitter and facebook

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:[email protected], 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!

like image 688
FlashAsh80 Avatar asked Sep 13 '15 11:09

FlashAsh80


2 Answers

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

like image 109
FlashAsh80 Avatar answered Oct 17 '22 12:10

FlashAsh80


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.

like image 35
mapm Avatar answered Oct 17 '22 10:10

mapm