How to trigger an event when user clicks away from EditText?

Viewed 74

I am building an app, and I want the app to trigger an event when the user has entered text into the editText and clicks away from it or closes the keyboard? How can I do this?

I am not very skilled in Java, so I would be grateful if you provided some description or code.

2 Answers

You can use onFocusChange listener on your edit text to overcome this problem

yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if(b==true){
                //entered in the edit text
            }
            else {
                //left edit text
            }
        }
    });

Simply set an onFocusChangedListener on your EditText and configure what happens when the EditText loses focus. Here's an illustration:

mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean bool) {
        if(bool){
            // here, the EditText is in focus
        }
        else {
            //here, the EditText is no longer in focus.
            // do what you want to do
        }
    }
});

I hope this helps.

Related