setSpan (4 ... 4) ends beyond length 0, this means you have a 4 white space character string its actual/trimmed length is zero, setting selection to this will cause IndexOutOfBoundsException, you should check the trimmed length before setting selection