0

Lets assume I have 2 EditText objects inp1 and inp2. If I enter something into inp1 I want it to appear in inp2 and vice versa. Changes made in either should change the other as well. i actually want two EditText objects to input numbers in bases 10 and 2 respectively. And when I enter a binary number I want its equivalent decimal number to appear in the other EditText and vice versa without the use of any button or anything. Is there anything equivalent to the onClick attribute of buttons for EditText? Which might call a function automatically whenever there is a change in the EditText's text. I hope I could make my question clear. Thank You.

Diptangsu Goswami
  • 5,554
  • 3
  • 25
  • 36

3 Answers3

0

You need to use the TextWatcher on both fields. The answer to this question basically does what you describe

Community
  • 1
  • 1
Joe Maher
  • 5,354
  • 5
  • 28
  • 44
0

you can use TextWatcher like this

tagNameEditText.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) {
                // set text or result in other EditText here   
                }

                @Override
                public void afterTextChanged(Editable s) {

                              }
            });

In this Listener in onTextChanged() method you can set the text in other EditText

Awadesh
  • 3,530
  • 2
  • 20
  • 32
0

Use TextWatcher. This is short code:

private final TextWatcher edit_one_Watcher = new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
         // enter your logic here 
//and print in second edittext
        }

        public void afterTextChanged(Editable s) {

        }
    };

And

private final TextWatcher edit_second_Watcher = new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
             // enter your logic here 
    //and print in first edittext 
            }

            public void afterTextChanged(Editable s) {

            }
        };  

Note: use boolean / flag to ignore autotextchange.

.i.e. boolean ignoreFirstTextChange = true;
.i.e. boolean ignoreSecondTextChange = true;
Govinda P
  • 3,261
  • 5
  • 22
  • 43
  • lets assume the inputs are inp1 and inp2. How do I initialize them to use the TextWatcher features? It tells me edit_one_Watcher and edit_second_Watcher are never used. – Diptangsu Goswami Mar 05 '16 at 14:52
  • inp1.addTextChangedListener(edit_one_Watcher ); inp2.addTextChangedListener(edit_second_Watcher ); – Govinda P Mar 07 '16 at 04:30