S Shah
S Shah

Reputation: 428

java.lang.IndexOutOfBoundsException: setSpan (118 ... 119) ends beyond length 118

I searched different questions but found nothing specific about my problem. I am changing text color by selecting color and works successfully however when I start deleting my edit text after typing a color text I get this error.

myedittext.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) {
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor(txtColor)), start, start+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}

I get the following error

java.lang.IndexOutOfBoundsException: setSpan (118 ... 119) ends beyond length 118
    at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1309)
    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:680)
    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:672)
    at com.apps.primalnotes.Fragments.EditorFragment$16.onTextChanged(EditorFragment.java:842)
    at android.widget.TextView.sendOnTextChanged(TextView.java:10611)
    at android.widget.TextView.handleTextChanged(TextView.java:10715)
    at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:14057)

I am writing in two colors now like this

enter image description here

now when i save it. it saves in pink color only and shows me like this enter image description here enter image description here but now when i save it again without any changes it save in the colors i wrote it enter image description here

Upvotes: 3

Views: 6989

Answers (2)

user5125586
user5125586

Reputation:

The onTextChanged method is invoked to tell you that within CharSequence s, number of character count beginning at start have just replaced old text that had length before.

What is happening is that when user presses backspace, start is at the upper limit of your charsequence i.e. If you had seven character before, start is 6 which is the same as the last element. You are doing start+1 which is always a number out of index range.

myedittext.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) {
            if(start < s.length() - 1 || count > before){
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor(txtColor)), start, start+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}

Didn't get to try that code but it should work. It's just to show you an idea of what you are doing wrong and what you should be doing.

Upvotes: 4

Ben P.
Ben P.

Reputation: 54194

When you delete text from the end of the string, the onTextChanged() method will be invoked with values representing the fact that the string is now shorter. It looks like you've written your code to always assume that the length will be longer; you're coloring in the just-typed character. But if you delete a character instead, you probably don't want to do this.

You could add a check to make sure that count is greater than before, this at the very least will indicate that text was added to the string.

if (count > before) {
    spannableString.setSpan(...);
}

Upvotes: 3

Related Questions