TokenCompleteTextView that accept free text next to chips

451 views Asked by At

I am trying to implement chips inside input field that beside chips can accept free text using library splitwise/TokenAutoComplete. One chip is @mention that shows user name.

User can enter sign @ and after that popup will show and than user selects user from list. The @ sign just triggers API call that fetches new data and updates adapter. After selecting user from list, chip will appear without @ sign, just chip with name. Also user can enter some free text and after that if he wants to mention another user he just enters @ and choose other user from list.

For example: user enters "Hello @mark and @paul", result should be "Hello Marc and Paul".

This works fine, but the problem is when i try to delete chips with backspace. I got exception:

E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.xxx.yyy, PID: 7859
 java.lang.IndexOutOfBoundsException: charAt: 12 >= length 12
     at android.text.SpannableStringBuilder.charAt(SpannableStringBuilder.java:116)
     at com.tokenautocomplete.TokenCompleteTextView$TokenTextWatcher.afterTextChanged(TokenCompleteTextView.java:1325)
     at android.widget.TextView.sendAfterTextChanged(TextView.java:9078)
     at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:11739)
     at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:976)
     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:520)
     at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:216)
     at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:33)
     at android.view.inputmethod.BaseInputConnection.deleteSurroundingText(BaseInputConnection.java:246)
     at android.view.inputmethod.InputConnectionWrapper.deleteSurroundingText(InputConnectionWrapper.java:66)
     at com.tokenautocomplete.TokenCompleteTextView$TokenInputConnection.deleteSurroundingText(TokenCompleteTextView.java:1561)
     at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:389)
     at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:145)
     at android.app.ActivityThread.main(ActivityThread.java:6938)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

I don't know how to fix it, and what is causing this exception.

Here is my implementation of Tokenizer:

class UsernameTokenizer implements MultiAutoCompleteTextView.Tokenizer {

@Override
public CharSequence terminateToken(CharSequence text) {
    int i = text.length();
    while (i > 0 && text.charAt(i - 1) == ' ') {
        i--;
    }
    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 + " ";
    }
}

@Override
public int findTokenStart(CharSequence text, int cursor) {
    int i = cursor - 1;
    while (i > 0 && text.charAt(i) != '@') {
        i--;
    }
    if (i < 1 || text.charAt(i) != '@') {
        return cursor;
    }
    return i;
}

@Override
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;
}

}

0

There are 0 answers