0

I want when a user types the email, the moment they press@ , it auto fills the rest to them as username@coretec.co.ke e.g but the problem is that its crashing

   @Override
        public void afterTextChanged(Editable s) {
          if(email.getText().toString().contains("@")){
              String e = email.getText().toString();
              email.setText(e+"coretec.co.ke");


          }

        }

Error logcat:

java.lang.StackOverflowError
     at java.lang.System.arraycopy(System.java:216)
     at android.text.SpannableStringBuilder.getChars(SpannableStringBuilder.java:926)
     at android.text.TextUtils.getChars(TextUtils.java:81)
     at android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
     at android.text.TextUtils.getChars(TextUtils.java:81)
     at android.text.TextUtils.indexOf(TextUtils.java:114)
     at android.text.StaticLayout.generate(StaticLayout.java:191)
     at android.text.DynamicLayout.reflow(DynamicLayout.java:288)
     at android.text.DynamicLayout.<init>(DynamicLayout.java:174)
     at android.widget.TextView.makeSingleLayout(TextView.java:6209)
     at android.widget.TextView.makeNewLayout(TextView.java:6107)
     at android.widget.TextView.checkForRelayout(TextView.java:6820)
     at android.widget.TextView.setText(TextView.java:3850)
     at android.widget.TextView.setText(TextView.java:3708)
     at android.widget.EditText.setText(EditText.java:81)
     at android.widget.TextView.setText(TextView.java:3683)
     at com.coretec.coretec.activity.Login$1.afterTextChanged(Login.java:79)
Maveňツ
  • 1
  • 12
  • 50
  • 89
Wanjala Alex
  • 141
  • 2
  • 13

3 Answers3

0

The afterTextChanged's documentation states,

This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively

One way to update the EditText from TextWatcher is to unregister the watcher from EditText first, set new values to EditText and finally register the watcher again at the EditText to handle further change.

Here is a good example of it.

Community
  • 1
  • 1
fluffyBatman
  • 6,524
  • 3
  • 24
  • 25
0

remove text change listener before adding your text else it will go in infinite loop. update your code like this:

@Override
        public void afterTextChanged(Editable s) {
          if(email.getText().toString().contains("@")){
              email.removeTextChangedListener(this);  // this line
              String e = email.getText().toString();
              email.setText(e+"coretec.co.ke");


          }

    }
Pushpendra
  • 2,791
  • 4
  • 26
  • 49
-1

Try this

String text = email.getText().toString();
if(text.contains("@") && !text.contains("@coretec.co.ke")){
    String e = email.getText().toString();
    email.setText(e+"coretec.co.ke");
}
Parvez M Robin
  • 154
  • 1
  • 3
  • 16