WebView: Keyboard doesn’t shows

José Castro
Devsys
Published in
1 min readJun 26, 2017

--

Oldie but goldie

In a WebView, on android 4.x (and after), when pressing on a text field (suppose we go to facebook page) should appear the keyboard to enter our user and password, but in previous versions of android, as Version 2.2 and 2.3, this does not happen. All this happens because the OS does not detect the focus on a text field so you have to do it manually:

myWebView.requestFocus(View.FOCUS_DOWN);
myWebView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});

In the first line we are already asking for the focus on our WebView (myWebView is a WebView object), then we are placing an onTouchListener event, which when you touch the WebView will get the event that is captured on the switch (either moving down or down , What matters is that you touch it) and with that we give the focus (requestFocus ()) to our view, remember that WebView inherits from View and is the one passed to the onTouch () method of the Listener.

--

--