-1

After click on the input field in android webview the keyboard appears. When I click on the back button I would like the keyboard to close. If the keyboard is closed and the back button is pressed, I need to go to the previous page. For example, Chrome behaves like this.

I did not find a reliable way to determine whether the keyboard is open or not. Maybe I'm going the wrong way and I can do without determining the openness of the keyboard manually?

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108

1 Answers1

0

Override onKeyDown method in your Web View Activity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)){
        hideSoftKeyboard();
    }
}

Use this to hide soft keyboard

InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
Vishal Arora
  • 1,908
  • 1
  • 8
  • 12
  • Thank you. This is almost what I need. But I want to know if the keyboard was closed after this, or it was already closed. This will help me decide whether to go to the previous page. – AntonFedorov Nov 06 '18 at 10:29
  • hideSoftInputFromWindow returns some boolean. Its meaning is not described in the [documentation](https://developer.android.com/reference/android/view/inputmethod/InputMethodManager#hideSoftInputFromWindow(android.os.IBinder,%20int)). But it looks like it returns whether the keyboard was closed. – AntonFedorov Nov 06 '18 at 10:33
  • hideSoftInputFromWindow has an overload with a ResultReceiver argument. – AntonFedorov Nov 07 '18 at 05:43