Search code examples
androidandroid-edittexttextwatcher

How to trigger when TextEdit value changed?


I tried this:

houseField.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            // 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 onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            Toast.makeText( getApplicationContext(), s, 15000).show();
        }
    });

But event triggered after each symbol inserted but I dont want that..

I need to trigger event when user leaves textedit and text is changed, becouse i will do query to google maps api. I can't do query after each character entered.


Solution

  • Have you tried onFocusChanged()

     yourTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
    
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
          if(hasFocus)
          {
            //do your logic here
          }
        }
     }
    

    The other option is to use onTextChanged to start a timer which checks to delay the process and reset if another key stroke happens. This approach may be more suitable to your application but is trickier to implement

    EDIT

    Alternatively you can override boolean View.onKeyPreIme(int keyCode, KeyEvent event) and capture the enter key ( but you will also need to think about the user tapping the back key to dismiss keyboard).