Changing characters in an EditText while typing? Android

2.4k views Asked by At

I currently have an app that is using a timer of sorts and I have an edit text that changes the time. I would like to add a feature that automatically inserts a colon or period after a certain number of characters has been typed in as the syntax for changing time is specific (it has to be XX:XX.X)

I've been playing around with it and it only gives me an error when I try to change the editText text. The error appears when I try to type.

I copy and pasted some code I found on here and changed it to fit my needs/

editClock = (EditText) findViewById(R.id.editClock);       
editClock.addTextChangedListener(new TextWatcher(){
    int count =1 ;
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
    {
        //System.out.println("beforeTextChanged::: => "+charSequence);
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        //System.out.println("onTextChanged::: => "+charSequence);
    }

    @Override
    public void afterTextChanged(Editable editable) {
        if(count==2)
            editClock.setText(editable.toString()+":");
        if(count==4)
            editClock.setText(editable.toString()+".");
        count++;     
    }
});

This is the error I get:

12-07 20:34:08.380: E/AndroidRuntime(13549): FATAL EXCEPTION: main
12-07 20:34:08.380: E/AndroidRuntime(13549): java.lang.StackOverflowError
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidtext.TextUtils.getChars(TextUtils.java:77)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidtext.TextUtils.indexOf(TextUtils.java:110)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidtext.StaticLayout.generate(StaticLayout.java:182)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidtext.DynamicLayout.reflow(DynamicLayout.java:284)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidtext.DynamicLayout.<init>(DynamicLayout.java:170)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.makeSingleLayout(TextView.java:6058)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.makeNewLayout(TextView.java:5956)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.checkForRelayout(TextView.java:6495)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3771)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at     androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549):    at an
1

There are 1 answers

2
Peri Hartman On BEST ANSWER

One way you can fix it is to put a class-level flag in your code. Set the flag when you are about to call setText() and clear it afterwards. Then, at the beginning of afterTextChanged(), check your flag. Something like this:

 @Override
 public void afterTextChanged(Editable editable) {
   if (overridingText)
     return;

   if(count==2)
   {
     overridingText = true;
     editClock.setText(editable.toString()+":");
     overridingText = false;
   }
   else if(count==4)
   {
     overridingText = true;
     editClock.setText(editable.toString()+".");
     overridingText = false;
   }

   count++;     
 }

Perhaps Google has provided a better way to do this. I don't know if it, though.