0

I got some issue with my code and I can't find anything to help me. I'm trying to clear my edittext when I'm typing any char. I got this error

/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: src.length=10 srcPos=10 dst.length=10 dstPos=0 length=2
    at java.lang.System.arraycopy(Native Method)
    at android.text.SpannableStringBuilder.moveGapTo(SpannableStringBuilder.java:178)
    at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:376)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:549)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:507)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:37)
    at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:843)
    at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:197)
    at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:177)
    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:339)
    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:89)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

And my code is like that

binding.formInputQuantity.editTextValue.addTextChangedListener(object: TextWatcher{
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                if(locationIsFocused){
                    locationIsFocused = false
                    binding.formInputQuantity.editTextValue.text.clear()
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }
        })
locationEditText.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            locationIsFocused = true
        }

When I launch the step by step, everything is working fine, but not when I run it normally.

Thanks !

Heia
  • 1
  • 1
  • It is not allowed to change the EditText from beforeTextChanged or onTextChanged. You can only make changes from the afterTextChanged function. See https://stackoverflow.com/questions/20278382/differences-between-textwatcher-s-ontextchanged-beforetextchanged-and-aftertex – Mario Huizinga Apr 04 '22 at 09:53
  • Problem is: I have to keep the car the user is typing. I tried to clear my edittext in the "ontextchanged" but I miss one character and don't know how to keep it (I'm open to do that if someone can tell me how :-D ) – Heia Apr 04 '22 at 10:08
  • Carefully check the example I linked and try to understand what is happening. You could save the CharSequence from onTextChanged in some variable and then use it in afterTextChanged, to change the contents of the EditText. – Mario Huizinga Apr 04 '22 at 10:14
  • What are you actually trying to achieve? Can you explain that? – Pronoy999 Apr 04 '22 at 10:15
  • My Edittext got some text in it. I want, when I start typing anything, the text gone. Example: Edittext = "Hello". I start typing "Bye" and, when the "B" is typed, "Hello" is deleted and my text is "B" (then, "ye") – Heia Apr 04 '22 at 10:31
  • @MarioHuizinga I am trying to right now :-) – Heia Apr 04 '22 at 10:34

0 Answers0