Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How handle EditText Change Listener in android

Imagine I have 5 EditText and I need to to populate values when one edittext value is changed. The values are interdependent. I am using the following logic to achieve that.

//Finding which edit text is originating values 

for(int i=0;i<mEditTextIds.length;i++){ 
        txt[i]=(EditText) findViewById(mEditTextIds[i]);
        txt[i].setOnTouchListener(new EditTextTouchLisner());
        txt[i].setId(i);
}


//Enable that Edittext's TextChangeListner and disabling all other Edittext's TextChangeListner ..

if (event.getAction() == MotionEvent.ACTION_DOWN){
    for(int i=0;i<mTableRowIds.length;i++){
        if(i!=v.getId()){
            txt[i].addTextChangedListener(null);
        }       
    }
    txt[v.getId()].addTextChangedListener(new TextChangedListner());


//calculating values 

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    //calculate();
    Toast.makeText(getApplicationContext(), "CHANGED", Toast.LENGTH_SHORT) .show(); 
}

But if I click edittext a second time I get the following errors:

10-25 16:23:05.149: ERROR/AndroidRuntime(2378): FATAL EXCEPTION: main
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): java.lang.NullPointerException
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.widget.TextView.sendBeforeTextChanged(TextView.java:6116)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.widget.TextView.access$700(TextView.java:187)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.widget.TextView$ChangeWatcher.beforeTextChanged(TextView.java:6309)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.text.SpannableStringBuilder.sendTextWillChange(SpannableStringBuilder.java:878)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:279)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:384)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:292)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:73)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.os.Looper.loop(Looper.java:123)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at java.lang.reflect.Method.invokeNative(Native Method)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at java.lang.reflect.Method.invoke(Method.java:521)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at dalvik.system.NativeStart.main(Native Method)
like image 656
vnshetty Avatar asked Oct 25 '11 11:10

vnshetty


1 Answers

Instead of implementing EditTextTouchLisner, you can implement addTextChangedListener to your EditText.

addTextChangedListener takes TextWatcher object as an argument

Here is a Help With TextWatcher

like image 131
Paresh Mayani Avatar answered Oct 05 '22 02:10

Paresh Mayani