1

I have this problem, how can I format the display on edittext as i type on it? for example if i type 5 it will format for desired currency. i can format the text in edittext without problem on onFocusChangeListener but the client needs onchange.

here are some code:

   <code>
    inputText = (EditText) findViewById(R.id.et_compare_rate_saving);
    inputText.addTextChangedListener(new TextWatcher() {
        @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(!editable.toString().isEmpty()){
                String amount = LO.removeSeparators(editable.toString(), getLocaleFormat());
                String newFormat = LO.compareRatesFormatting(Double.valueOf(amount),getLocaleFormat());
                System.out.println("afterTextChanged::: => "+newFormat);
                inputText.setText(newFormat);
            }
            //System.out.println("afterTextChanged::: => "+editable.toString());
        }
    });

   </code>

im having a stackoverflow error

  <code>
    08-01 22:37:32.724: ERROR/AndroidRuntime(1566): FATAL EXCEPTION: main
    java.lang.StackOverflowError
    at android.view.View.addFocusables(View.java:4311)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:712)
    at android.view.View.getFocusables(View.java:4269)
    at android.view.FocusFinder.findNextFocus(FocusFinder.java:114)
    at android.view.FocusFinder.findNextFocus(FocusFinder.java:98)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:570)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572)
    at android.view.View.focusSearch(View.java:4192)
    at android.widget.TextView.onCreateInputConnection(TextView.java:4936)
    at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:964)
    at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:919)

    </code>

Any idea? cheers. thanks a lot.

Eman
  • 55
  • 9

2 Answers2

2

To shed a little more light on the situation, the documentation for TextWatcher explicitly states that you should take care when changing text in afterTextChanged:

public abstract void afterTextChanged (Editable s)

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. (You are not told where the change took place because other afterTextChanged() methods may already have made other changes and invalidated the offsets. But if you need to know here, you can use setSpan(Object, int, int, int) in onTextChanged(CharSequence, int, int, int) to mark your place and then look up from here where the span ended up.

gyoda
  • 1,053
  • 2
  • 11
  • 22
  • thanks for reply...i find this http://android-sdk.appspot.com/reference/android/telephony/PhoneNumberFormattingTextWatcher.html which a utility to watch a textview upon change on of text. is it possible to do similar to this on edittext itself? Thanks – Eman Aug 04 '12 at 04:27
  • i find this post helpful http://stackoverflow.com/questions/5107901/better-way-to-format-currency-input-edittext – Eman Aug 06 '12 at 12:49
0

You're in an infinite loop and that's why you get the stackoverflow. Everytime the text changes, your afterTextChanged gets called, which changes the text and causes it to be called again

Martin
  • 4,711
  • 4
  • 29
  • 37