Android AutoCompleteTextView with '@' mentions filtering like twitter and facebook
Asked Answered
Q

2

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:[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!

Quell answered 13/9, 2015 at 11:58 Comment(0)
Q
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

Quell answered 16/9, 2015 at 15:3 Comment(6)
how you obtain a string with this format? : "This is a message for [username:[email protected], id:44]."Beane
@FlashAsh99 Could you please tell how you did replacing of the text after using spyglassExcurved
@FlashAsh99 I too want to know how did you replace the text with spyglassPleonasm
@FlashAsh99: Right now, the code in git currently gets the user/city date from raw file. Is there any way to load dynamic array for this ?Mccubbin
Answer to @AnaLlera?Ajay
I have also used the same lib. But in my case edit text is at the bottom. The pop up window covers up the keyboardBoatswain
M
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.

Mann answered 13/9, 2015 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.