1

I am using an EditText field in my application. also i had added addTextChangedListener for that. On the onTextChanged method, i had called my squareFunction. The code is like..

value = (EditText)findViewById(R.id.amount);
value.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
            {
                squareFunction();

            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
                // TODO Auto-generated method stub

            }

            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub

            }
        });

Now my problem:= i am getting the value of input numbers correctly. But when i clear the EditField, an error comes and ask me to "Force Close". I want to make my editField value to 0 if all the characters in it are cleared. Is it possible? If yes, how it can be done. Plz explain with code if possible...

Dil Se...
  • 877
  • 4
  • 16
  • 43

3 Answers3

2

You should try adding condition like

if(value.getText().toString().equalsIgnoreCase(""))
{
 Log.i(TAG,"EditText Clear");
//set value to 0
}
else
{
 squareFunction(); 
}

also use this in afterTextChanged

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
2

inside onTextChanged()

if( arg0.length()==0)
{
value.setText("");
}

i hope you tried this basic thing , so what errorlog says at the time of force close ??

Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40
  • sorry dear. i am new to android. im just studying the basics. i had tried that logic. but instead of that arg0.length()==0, i had checked with arg0.length()==1. thank u dear for ur timely help... – Dil Se... Dec 06 '11 at 06:25
0

The error is due to the function trying to perform square when you r clearing the values. Move the square function to afterTextChanged, and in onTextChange, check the length of the edittext, if it is zero, set text to 0.

Mal
  • 373
  • 4
  • 15