182

I need to catch when an EditText loses focus, I've searched other questions but I didn't find an answer.

I used OnFocusChangeListener like this

OnFocusChangeListener foco = new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub

    }
};

But, it doesn't work for me.

dkmann
  • 611
  • 2
  • 8
  • 18
japuentem
  • 2,117
  • 3
  • 14
  • 13

5 Answers5

387

Implement onFocusChange of setOnFocusChangeListener and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control.

 EditText txtEdit = (EditText) findViewById(R.id.edittxt);

 txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {          
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
               // code to execute when EditText loses focus
            }
        }
    });
Mann
  • 560
  • 3
  • 13
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 22
    +1 - but worth noting that if your edit text is in a listview, every key down will result in the box losing and getting focus. See this solution to keep track of the currently focused box: http://stackoverflow.com/questions/9527067/android-edittext-in-listview-loses-focus-on-calling-notifydatachanged – bsautner May 02 '13 at 14:18
  • Where do I add the code you show? If I put it as is in "onCreate" the app crashes – Jona Jun 22 '14 at 16:20
  • @Léon Pelletier The truth? Really? User touches another focusable control and the editText loses it. I can't see any problem. It's the same if you set the focus by your code. – The incredible Jan May 23 '18 at 09:43
  • @Jona You could add it anywhere but onCreate() works. If it crashes you do something wrong. – The incredible Jan May 23 '18 at 09:45
10

Have your Activity implement OnFocusChangeListener() if you want a factorized use of this interface, example:

public class Shops extends AppCompatActivity implements View.OnFocusChangeListener{

In your OnCreate you can add a listener for example:

editTextResearch.setOnFocusChangeListener(this);
editTextMyWords.setOnFocusChangeListener(this);
editTextPhone.setOnFocusChangeListener(this);

then android studio will prompt you to add the method from the interface, accept it... it will be like:

@Override
public void onFocusChange(View v, boolean hasFocus) {
  // todo your code here...
}

and as you've got a factorized code, you'll just have to do that:

@Override
public void onFocusChange(View v, boolean hasFocus) {
  if (!hasFocus){
    doSomethingWith(editTextResearch.getText(),
      editTextMyWords.getText(),
      editTextPhone.getText());
  }
}

That should do the trick!

Cerberus
  • 137
  • 2
  • 10
  • At the time, I just wanted to make things very clear, but as I said it's not usable as it is anyway... – Cerberus Nov 02 '18 at 18:43
9

Kotlin way

editText.setOnFocusChangeListener { _, hasFocus ->
    if (!hasFocus) {  }
}
Achraf Amil
  • 1,275
  • 16
  • 20
2

Using Java 8 lambda expression:

editText.setOnFocusChangeListener((v, hasFocus) -> {
    if(!hasFocus) {
        String value = String.valueOf( editText.getText() );
    }        
});
Sandeep Yohans
  • 929
  • 1
  • 14
  • 36
1

Its Working Properly

EditText et_mobile= (EditText) findViewById(R.id.edittxt);

et_mobile.setOnFocusChangeListener(new OnFocusChangeListener() {          
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            // code to execute when EditText loses focus
            if (et_mobile.getText().toString().trim().length() == 0) {
                CommonMethod.showAlert("Please enter name", FeedbackSubmtActivity.this);
            }
        }
    }
});



public static void showAlert(String message, Activity context) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(message).setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            });
    try {
        builder.show();
    } catch (Exception e) {
        e.printStackTrace();
    }

}
YoussefDir
  • 287
  • 1
  • 3
  • 16
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53