0

On some devices, with this code, the EditText to_quantity is not updated in real time during the insertion of the value to be converted in the EditText from_quantity. What can be the problem?

EDIT TEXT from_quantity:

from_quantity.setOnKeyListener(new OnKeyListener(){

    public boolean onKey(View v, int keyCode, KeyEvent event){

        try{
        if(!(Double.isNaN(Double.valueOf(from_quantity.getText().toString())))){
                                                  convert(from_quantity.getText().toString(), "to");
        }
        }catch(NumberFormatException e){
            Log.d("error", e.toString());
        }
            return false;
        }
        });

            from_quantity.setOnTouchListener(new OnTouchListener(){

                            public boolean onTouch(View v, MotionEvent event) {
                                    //Clear Quantities
                                    from_quantity.setText("");
                                    to_quantity.setText("");
                                    return false;
                            }

            });

EDIT TEXT to_quantity:

to_quantity.setOnKeyListener(new OnKeyListener() {
     public boolean onKey(View v, int keyCode, KeyEvent event) {
         try{
            if(!(Double.isNaN(Double.valueOf(to_quantity.getText().toString())))) {
                  convert(to_quantity.getText().toString(), "from");
            }
          } catch(NumberFormatException e) {
               Log.d("error", e.toString());
          }
          return false;
     }
});


to_quantity.setOnTouchListener(new OnTouchListener(){
      public boolean onTouch(View v, MotionEvent event) {
          //Clear Quantities
          from_quantity.setText("");
          to_quantity.setText("");
          return false;
      }

});
bisemanu
  • 441
  • 2
  • 9
  • 19
  • What do you want to achieve bro? you can use on focus change listener on edit text and do stuffs there realtime when edittext gain or lose focus. – Marko Niciforovic May 10 '13 at 10:11

3 Answers3

0

If you use the soft keyboard of your device, the OnKeyListener will not be called. You should use a TextWatcher.

onKeyListener not working with soft keyboard (Android)

Community
  • 1
  • 1
Vincent Ketelaars
  • 1,069
  • 1
  • 14
  • 35
0

Some software keyboards do not launch onKey() events. Create a TextWatcher and set it as a listener to the EditText instead:

to_quantity.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String text = s.toString() ;
        try{
            if(!(Double.isNaN(Double.valueOf(text)))) {
                convert(text, "from");
            }
        } catch(NumberFormatException e) {
            Log.d("error", e.toString());
        }
    }

    @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

    }
});
tbkn23
  • 5,205
  • 8
  • 26
  • 46
  • but in this way the two EditText, from_quantity and to_quantity, are not coordinated ie the added is distorted – bisemanu May 11 '13 at 11:11
  • I don't understand what it means not coordinated... Use a `TextWatcher` in both `EditText` fields, it's the same as what you were trying to do except that I used a `TextWatcher` instead of `OnKeyListener`. If the code inside it doesn't do what it was supposed to do, that's a different matter. – tbkn23 May 11 '13 at 11:50
  • practically, in from_quantity I enter the number to convertitre and at the same time it is converted to to_quantity. But using the TextWatcher, the cursor is not simultaneous with edidtext to_quantity – bisemanu May 11 '13 at 12:38
0

if you wanna use soft keyboard you need to setFocusable to target view:

setFocusableInTouchMode(true); //Enable soft keyboard on touch for target view

setFocusable(true); //Enable hard keyboard to target view