Spannable is crashing app when accessibility is on? Need edittext setSelection alternative.

  • Thread starter Thread starter Android Central Question
  • Start date Start date
A

Android Central Question

I have an edittext for date and it automatically adds a slash for the date. To do so, I have a text change listener with this code in it.

if (currentTextLength == 2 || currentTextLength == 5) {
editText.setText("$s/")
editText.setSelection(editText.text.length)
}
setSelection places the cursor at the end of the text but it is taking the value as Spannable(if you look at its implementation) which will crash when Accessibility talkback is turned on.

The crash exception is IndexOutOfBoundException caused by Spannable when accessibility talkback is on. Spannable keeps crashing apps as far as I know because it happens too when I use SpannableString.

Is there a way to place the cursor at the end of the edittext without using setSelection? I tried append but it is not placing the cursor at the end.

Thanks in advanced.
 
[Moved to Software Development for better exposure]

Have you tried a two line app

Code:
editText.setText("abc")
editText.setSelection(editText.text.length)

to see what happens? There could be a bug in the code underlying setSelection or in talkback. Libraries have been known to have bugs. (That would require feedback to Google, I guess.)