androideditfield

how to set a default value to EditField when it is cleared?


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...


Solution

  • 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 ??