sonics876
sonics876

Reputation: 957

Clear EditText text after adding onTextChanged implemenation

I'm adding a listener to an EditText field to appropriately format the number to currency in real time.

        loanText.addTextChangedListener(new TextWatcher() {
        private String current = "";
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(!s.toString().equals(current)){
               String cleanString = s.toString().replaceAll("[$,.]", "");

               double parsed = Double.parseDouble(cleanString);
               String formated = NumberFormat.getCurrencyInstance().format((parsed/100));

               current = formated;
               loanText.setText(formated);
               loanText.setSelection(formated.length());
            }
        }
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }
    });

However, when I try to clear the EditView field, my App will crash. In addition, the backspace key no longer functions.

Upvotes: 14

Views: 25721

Answers (5)

Harish_N
Harish_N

Reputation: 2257

You can check whether Text is empty in onTextChanged as follows and do nothing if it is empty

s.toString().isEmpty()

public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(!s.toString().equals(current) && !s.toString().isEmpty()){
           String cleanString = s.toString().replaceAll("[$,.]", "");

           double parsed = Double.parseDouble(cleanString);
           String formated = NumberFormat.getCurrencyInstance().format((parsed/100));

           current = formated;
           loanText.setText(formated);
           loanText.setSelection(formated.length());
        }
    }

Upvotes: -1

Abakasha Panda
Abakasha Panda

Reputation: 348

public class MainActivity extends Activity {
EditText et;
TextWatcher tw;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et=(EditText) findViewById(R.id.et);

     tw=new TextWatcher(){
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            et.removeTextChangedListener(tw);
            et.setText(Math.random()+"");//add your logic here 
            et.addTextChangedListener(tw);
        }};
        et.addTextChangedListener(tw);
}   

} This is a Simple solution.

Upvotes: 1

2red13
2red13

Reputation: 11227

Try to use afterTextChanged instead. I also had many problems with the other one.

Upvotes: 4

MataMix
MataMix

Reputation: 3296

I found this class that works great for me: http://www.java2s.com/Code/Android/UI/ConvertinputvaluetoCurrencyinTextWatcher.htm

hope that will save you guys some time.

Upvotes: 1

james
james

Reputation: 26271

The problem is that your TextWatcher sees that you are "clearing" the text, and therefore sends off a request to its callback methods(afterTextChanged, beforeTextChanged, etc).

What i've done is to simply remove the TextWatcher, clear the text, and add the TextWatcherback to the EditText. That way, nothing is listening to my EditText changes.

You will probably have to hold on to an instance of the TextWatcher instead of inlining it like you did. That way you don't have to create one every time you want to clear the EditText

  1. loanText.removeTextChangedListener(yourTextWatcherObject);
  2. loanText.setText("");
  3. loanText.addTextChangedListener(yourTextWatcherObject);

Upvotes: 21

Related Questions