Tapping form field in WebView does not show soft keyboard

I created my own WebView and set the WebChromeClient and WebViewClient objects. When I start this WebView, the HTML form fields react when I touch them (a cursor appears), but they do not get selected, nor does the soft keyboard start. If I use the trackball to choose the form and press it, the keyboard does appear.

I tried to call myWebview.requestFocusFromTouch() as this answer suggested, but it returns false and doesn't help.


Solution 1:

http://code.google.com/p/android/issues/detail?id=7189

Here is a fix in case other were not clear.

    webview.requestFocus(View.FOCUS_DOWN);
    webview.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;
        }
    });

Solution 2:

Agreed 10% with Dv_MH's answer. However, the attached class was a little excessive. All I needed to do was extend WebView & return true for onCheckIsTextEditor()

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

public class LiveWebView extends WebView {

    public LiveWebView(Context context) {
        super(context);
    }

    public LiveWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LiveWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onCheckIsTextEditor() {
        return true;
    }
}

From there, I was able to use it as a normal WebView (even in Dialogs).

Alternatively, with less code:

WebView web = new WebView(context) {
  @Override
  public boolean onCheckIsTextEditor() {
    return true;
  }
};

Solution 3:

IMPORTANT I spent hours searching for this and I solved it by making sure that parent layout that extends "ViewGroup" doesn't have property

android:descendantFocusability = "blocksDescendants" which prevent child layout from getting focused

OR Add android:focusable="true" to webview