Published 19 Sep, 2022

Java - java.lang.IndexOutOfBoundsException: setSpan (N ... N) ends beyond length 10500

Category Java
Modified : Oct 04, 2022

I get this exception when I call EditText.setSelection()

java.lang.IndexOutOfBoundsException: setSpan (N ... N) ends beyond length 10500

N can be any number. For example if I call setSelection(10476,10568) N will be 10568. But 10500 is constant.

          java.lang.IndexOutOfBoundsException: setSpan (10568 ... 10568) ends beyond length 10500
              at android.text.SpannableStringBuilder.checkRange(
              at android.text.SpannableStringBuilder.setSpan(
              at android.text.SpannableStringBuilder.setSpan(
              at android.text.Selection.setSelection(
              at android.widget.EditText.setSelection(

Are there any limitations of EditText that cause this?

Before I do a selection I load a text file which contains definitely more than 10500 characters as it contains ca. 12000 lines. And then I call EditText.setText() to put the file contents into the field. EditText.setText() works without any problems. First I thought there is something wrong with the file contents, but I loaded another file and when the selection occurred I got the same exception and it contained ends beyond length 10500


There are 2 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Java, Android, Android Edittext. These have been categorized in sections for a clear and precise explanation.


This is the actual method from android.text.SpannableStringBuilder where your code is failing.

private void checkRange(final String operation, int start, int end) {
    if (end < start) {
        throw new IndexOutOfBoundsException(operation + " " +
                region(start, end) + " has end before start");

    int len = length();

    if (start > len || end > len) {
        throw new IndexOutOfBoundsException(operation + " " +
                region(start, end) + " ends beyond length " + len);

    if (start < 0 || end < 0) {
        throw new IndexOutOfBoundsException(operation + " " +
                region(start, end) + " starts before 0");

It looks like your start/end is greater than the length (calculated by length()) in the EditText. Probably a case of truncation.

As far as number of characters an EditText can hold is not restricted from Android OS, but probably from device. See Max 9000 characters in Android TextView? and also!topic/android-developers/JVAm8vBaIQg


I had such a problem when using AutoCompleteTextView. The problem is that the auto-completion window opens with a delay. If during this delay you delete the text, then when you try to open the window, the popup tries to work with the already deleted text. The only way to avoid this is to cancel opening this window. This can be done by removing all operations from the Handler of this view. autoCompleteTextView.getHandler().removeCallbacksAndMessages(null);