Sahidul Islam
Sahidul Islam

Reputation: 89

EditText On update/onChange value Add listener

I have 2 EditText in my activity,lets say editText1 ,editText2 and A Double Variable d=200

I want when user change/insert value for editText1 ,editText2 value will update in real Time as editText1*d

Also when user change/insert value for editText2 ,editText1 will be update in real time as editText2*d

I tried to use addTextChangedListener->onTextChangedbut it works fine for one Edit text,when I set this function for both editText then application crash because its creating an infinite loop,how can I solve this problem?

update :bellow is my code

et1.addTextChangedListener(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) {

                            Editable a = et1.getText();
                            Double aa = Double.parseDouble(a.toString())*100;
                            String aaa = aa.toString();

                            et2.setText(aaa);

                        }

                        @Override
                        public void afterTextChanged(Editable s) {

                        }
                    });

                    et2.addTextChangedListener(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) {

                            Editable b=et2.getText();
                            Double bb=Double.parseDouble(b.toString());
                            String bbb=bb.toString();
                            et1.setText(bbb);


                        }

                        @Override
                        public void afterTextChanged(Editable s) {

                        }
                    });

Upvotes: 0

Views: 250

Answers (3)

A P
A P

Reputation: 2548

You should implement a TextWatcher, It works pretty much like how it sounds. When the user changes (adds/removes text from) what is existing in the EditText, it will "Activate" this class. To test it out, you can use logs or set a breakpoint at any one of these statements.

private class MyTextWatcher implements TextWatcher {

    private View view;

    private MyTextWatcher(View view) {
        this.view = view;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
         //Your action here
          edit = fromEditText.getText().toString();

    }

    @Override
    public void afterTextChanged(Editable s) {
    }
}

and in onCreate, add this to connect your EditText to the TextWatcher

fromEditText.addTextChangedListener(new MainActivity.MyTextWatcher(fromEditText));

You can add a bunch of EditText's to a single TextWatcher. All you need to do is rinse and repeat. Just change the name of the respective EditText and add it to the TextWatcher. Be aware, that if you have some EditText change the text of another EditText, it can result in an infinite loop.

Upvotes: 0

Danyi
Danyi

Reputation: 21

EditText et1, et2;

TextWatcher watcher1 = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        Editable a = et1.getText();
        Double aa = Double.parseDouble(a.toString())*100;
        String aaa = aa.toString();
        et2.removeTextChangedListener(watcher2);
        et2.setText(aaa);
        et2.addTextChangedListener(watcher2);
    }

    @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

    }
};

TextWatcher watcher2 = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        Editable b=et2.getText();
        Double bb=Double.parseDouble(b.toString());
        String bbb=bb.toString();
        et1.removeTextChangedListener(watcher1);
        et1.setText(bbb);
        et1.addTextChangedListener(watcher1);
    }

    @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

    }
};
et1.addTextChangedListener(watcher1);
et2.addTextChangedListener(watcher2);

Upvotes: 1

ismail alaoui
ismail alaoui

Reputation: 6073

i think you have to create textwatcher separately then in your TextWatcher_EdOne

 TextWatcher  TextWatcher_EdOne   = new TextWatcher() {

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    //disable editext2 textwatcher 
                    et2.removeTextChangedListener(TextWatcher_EdTwo);

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                       ///do your stuff


                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                     // enable textwatcher on et2 again                                
                     et2.addTextChangedListener(TextWatcher_EdTwo);

                    }
                }

and will do it for the other textWatcher , this way you will avoid infinite loop

Upvotes: 0

Related Questions