0

I am making a converter app that converts rupees to dollar and vice versa,i want to use two edit text only, i want it to work as when i enter a value in the edit text meant for rupees it should simultaneously show the converted value in the edit text meant for dollar, to be more precise i want that if i want to convert 120 rupees to dollar, as i start entering 120, as 1 it should show the converted value simultaneously in the dollar edit text, followed by 12 and then finally 120.

I have tried-

if(editText.isFocusable()==true){

           editText.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) {

                   str=editText.getText().toString();
                   rs = Double.parseDouble(str);
                   rstodol = rs / 65;
                   str1 = Double.toString(rstodol);
                   editText1.setText(str1);
               }

               @Override
               public void afterTextChanged(Editable s) {

               }
           });


       }


        else if(editText1.isFocusable()==true){

           editText1.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) {

                   str=editText1.getText().toString();
                   dol=Double.parseDouble(str);
                   doltors=dol*65;
                   str2=Double.toString(doltors);
                   editText.setText(str2);
               }

               @Override
               public void afterTextChanged(Editable s) {

               }
           });
       }

where am i wrong ?

Destrif
  • 2,104
  • 1
  • 14
  • 22
Prime
  • 3
  • 2

1 Answers1

1

Listeners should be added once, then they perform their task until they are removed. You are only adding one listener due to your outer if block. Instead, you want to check if the currently selected EditText is hasFocus():

editText = (EditText)findViewById(R.id.edittext);
editText1 = (EditText)findViewById(R.id.edittext1);

editText.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) {

        if(editText.hasFocus()) {
            try {
                String str = editText.getText().toString();
                double rs = Double.parseDouble(str);
                double rstodol = rs / 65;
                String str1 = Double.toString(rstodol);
                editText1.setText(str1);
            }catch(NumberFormatException e){
                // invalid string entered
                editText1.setText("");
            }
        }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });


editText1.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) {
        if(editText1.hasFocus()) {
            try {
                String str = editText1.getText().toString();
                double dol = Double.parseDouble(str);
                double doltors = dol * 65;
                String str2 = Double.toString(doltors);

                editText.setText(str2);
            }catch(NumberFormatException e){
                // invalid string entered
                editText.setText("");
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});
bradkratky
  • 1,577
  • 1
  • 14
  • 28
  • One more thing related to it, can we use onKeyListener here ? – Prime Jul 09 '16 at 18:39
  • I've always used `TextWatcher`, which I believe is more specific because it reacts to when the text is changed. I don't have much experience with it but it seems onKeyListener is a method of View which detects hardware key input - other answers suggest it's less reliable http://stackoverflow.com/questions/24425838/edittext-onkeylistener-not-working-solved-with-workaround – bradkratky Jul 09 '16 at 19:05