0

I am calling clearFocus on the edit text when the user clicks the done button. However, when I do this the app gains focus on the container and a keyboard appears. When I add a dummy View, it gains focus on that view but a keyboard still appears! This keyboard doesn't do anything as well.

reconman
  • 195
  • 2
  • 10

1 Answers1

0

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your focused view.

// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.HIDE_IMPLICIT_ONLY as the second parameter to ensure you only hide the keyboard when the user didn't explicitly force it to appear (by holding down menu).

Note: If you want to do this in Kotlin, use:

context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

See more answers on this thread Hide Keyboard

Meriam
  • 855
  • 8
  • 17