-1

I have declared in my fragment a textInputLayout which contains an edit text with parameters

     android:inputType="textCapCharacters"
     android:maxLength="3"
     android:maxLines="1"

and it works correctly on most devices, it "survives" while user is trying to input more than 3 characters, but IF user tries to paste a string, which contains more than 3 characters it ends up crashing.

"java.lang.IndexOutOfBoundsException: setSpan (0 ... 10) ends beyond length 3" enter image description here

and just to be clear, the app doesn't crash after pasting longer strings on most of the devices but on some it does, and thus is why I'm here, is there any way I could prevent that?

JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
  • You need to create an event listner for paste action, and whenever user pate the copied text. Your custom event listner algorithm can reduce it to 3 words. – nilesh14k Mar 31 '22 at 03:17
  • 1
    there isn't really an easy way of implementing this [link](https://stackoverflow.com/questions/14980227/android-intercept-paste-copy-cut-on-edittext) – JustSightseeing Mar 31 '22 at 16:00

2 Answers2

1

Please refer this question. (When setting the Edit cursor, when measuring the Edit content length, it is 0, that is, the measured content is not found.)

Lasith M
  • 48
  • 7
1

You Could use the CLIPBOARD_SERVICE on your fragment as stated here , enable the editing (only if it's plain text), finally do a string.take(3) and assign that to your text property on the textInputLayout.