Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable TextWatcher on setting data to edittext

I am using TextWatcher on EditText .when I input something in edittext it works perfectly but when I set data into edittext from database getting NullPointerException. I used onTouchListener to on this Edittext. my question is. could I check the condition that TextWatcher works only after touch event is true.

EditText det=(EditText) findViewById(R.id.det);
    det.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            System.out.println(v.getId());
            d_id=v.getId();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                getPopUp(det);
            }
            return false;
        }
    });

when I call setText on det it goes to TextWatcher

det.setText(ft.get_det().toString());

TextWatcher

                    @Override
            public void afterTextChanged(Editable string) {
                try {

                    int rp=0;
                    det=(EditText)findViewById(d_id);
                    s_id=d_id-4;
                    sev=(EditText)findViewById(s_id);
                    o_id=d_id-2;
                    occ=(EditText)findViewById(o_id);
                    rp=d_id+1;
                    rpn=(TextView)findViewById(rp);
                    if (sev.getText().length() > 0
                            || occ.getText().length() > 0
                            || sev.getText().length() == 0) {
                        if (string.length() > 0) {
                            int s, o, d, r;
                            System.out.println(sev.getId());
                            System.out.println(det.getId());
                            s = Integer.parseInt(sev.getText().toString());
                            o = Integer.parseInt(occ.getText().toString());
                            d = Integer.parseInt(det.getText().toString());
                            r = s * o * d;
                            String m = r + "";
                            rpn.setText(m);                         
                        } else {
                            // det.setText("");
                            // rpn.setText("");
                             System.out.println("In else");
                        //  makeToast();
                        }
                    }

                } catch (Exception e) {
                    det.setText("");
                    rpn.setText("");
                    System.out.println("IN Exception");
                    makeToast();
                }
            }
        };
like image 377
sunil_patidar Avatar asked Oct 27 '13 06:10

sunil_patidar


People also ask

How do I make EditText disabled?

To disable an EditText while keeping this properties, just use UI. setReadOnly(myEditText, true) from this library. If you want to replicate this behaviour without the library, check out the source code for this small method.

How do I edit EditText Uneditable?

android:editable="false" should work, but it is deprecated, you should be using android:inputType="none" instead. Alternatively, if you want to do it in the code you could do this : EditText mEdit = (EditText) findViewById(R. id.

How do you make EditText read only?

The most reliable way to do this is using UI. setReadOnly(myEditText, true) from this library. There are a few properties that have to be set, which you can check out in the source code.

How do you make EditText editable?

We can set editable property of EditText in XML layout but not programatically, but there is no setEditable() method! If EditText is not Enabled [ by setEnabled(false) ] it still Editable!


2 Answers

Create a TextWatcher like below

TextWatcher textWatcher=new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    };

and then to add this to your EditText call

   EditText.addTextChangedListener(textWatcher);

and to remove call

   EditText.removeTextChangedListener(textWatcher);
like image 111
Jitender Dev Avatar answered Oct 21 '22 04:10

Jitender Dev


i solved this by taking a global boolen variable.Set this variable true in onTouchListener and check condition in afterTextChanged method that if varible is true of false.

like image 43
sunil_patidar Avatar answered Oct 21 '22 04:10

sunil_patidar