0

I have EditText which is connected with TextWatcher I'm monitoring when user presses @ letter. That will make a Listview appear with names of commentators on particular post. When user chooses one of the users from ListView, name is append to EditText and ListView is hidden.

But the problem is when user continues typing ListView will appear again because afterTextChanged(Editable s) monitors the whole inputted text which already contains letter @.

Is there a way to monitor only what user is actually typing not the whole inputed text? Or somehow escape last inputed word in TextWatcher? Or any other suggestions how to solve this.

I was researching but didn't find anything useful. Thanks in advance

Yupi
  • 4,402
  • 3
  • 18
  • 37

1 Answers1

0

There can be many ways to achieve this. This is one of them. You can check weather your list is already filled or not.

  final boolean isListSet = false;
        public static final String textToFind = "@";
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!isListSet && s.toString().contains(textToFind)) {
                    // set your list here
                    isListSet = true;
                }
                if (!s.toString().contains(textToFind)) {
                    // remove your list
                    isListSet = false;
                }
            }
        });
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • This solves one problem but raises another. What if user want to input more than one commentators for example `@John, @Lisa` etc? This will work only if there is going to be one mentioning in comment for more `ListView` will not appear again because `isListSet` is `true` – Yupi Jun 02 '18 at 13:46
  • I did not think that. I will modify this method, wait a bit. – Khemraj Sharma Jun 02 '18 at 13:48
  • I was thinking to write logic for this. But here are many things to manage . I can write this for you. But here is an library that would be beneficial for you. https://github.com/linkedin/Spyglass – Khemraj Sharma Jun 02 '18 at 13:54
  • I suggest you use this library if you want to boost up things – Khemraj Sharma Jun 02 '18 at 13:55