How to Know When an Edittext Loses Focus

How can I know when an EditText loses focus?

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
}
}
});

Kotlin - How to know if editText lost focus

This way

// Class level
lateinit var editText: EditText

// Method onCreate/onCreateView
editText = findViewById(R.id.whatever)

editText.setOnFocusChangeListener { view, hasFocus ->
if(!hasFocus) {

}
}

EditText loses focus when keyboard appears; requires touching twice to edit

A solution is to post a delayed runnable that checks to see if the EditText view is still focused, and if it is not, focus it.

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(final View v, boolean hasFocus) {
if (hasFocus) {
// Request focus in a short time because the
// keyboard may steal it away.
v.postDelayed(new Runnable() {
@Override
public void run() {
if (!v.hasFocus()) {
v.requestFocus();
}
}
}, 200);
}
}
});

Numeric edittext loses focus after first input in listview

I managed to fix it, after debugging the currentFocus i found out that the listview onExpand() took the focus away, setting editText.requestfocus() after returned the focus to the edittext.

EditText focus lost

Focusable EditText inside ListView

Force EditText to lose focus when: some keyboard keys are pressed and when user clicks on something else in the activity

To make editText lose focus when you press outside of the keyboard you can try to setOnTouchListener to the view that is visible when the keyboard is shown. For example, it might be the parent layout, listView, recyclerView or any other significant in size view. In order to do that, just add code below inside of your onCreate method in activity:

findViewById(R.id.loginLayout).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
usernameEditText.clearFocus();
passwordEditText.clearFocus();
return false;
}
});

To make editText lose focus and/or hide keyboard when pressing some button on keyboard you can use the following code. There is an example of listener for Enter key. You may find all the other keys on official documentation.

yourEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP) {
yourEditText.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
return false;
}
});


Related Topics



Leave a reply



Submit