When I display a WebView
, I don\'t see the soft keyboard popping up. The hard keyboard also doesn\'t work!
What are the usual shortcomings.
The
The full solution is a bit more than what Sana had. It is documented as a bug over at the Android site ( http://code.google.com/p/android/issues/detail?id=7189 ):
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;
}
});
The problem was that webview wasn't getting focus when it was loaded hence using
webView.requestFocus(View.FOCUS_DOWN);
solved the problem.
Had the same issue and non of the above solutions worked. This woked for me
<CustomWebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="true" />
I'm surprised that even on Android 4.1 (tested on SGS3) the problem is still present, and overriding onTouch() don't solve it for me.
Test code:
String HTML = "<html><head>TEST</head><body><form>";
HTML += "<INPUT TYPE=TEXT SIZE=40 NAME=user value=\"your name\">";
HTML += "</form></body></html>";
WebView wv = new WebView(getContext());
wv.loadData(HTML, "text/html", null);
final AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
adb.setTitle("TEST").setView(wv).show();
My complex solution is replace WebView with MyWebView:
private static class MyWebView extends WebView
{
public MyWebView(Context context)
{
super(context);
}
// Note this!
@Override
public boolean onCheckIsTextEditor()
{
return true;
}
@Override
public boolean onTouchEvent(MotionEvent ev)
{
switch (ev.getAction())
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!hasFocus())
requestFocus();
break;
}
return super.onTouchEvent(ev);
}
}
I had the same problem on Jelly Bean, but none of the above solutions worked for me. This solution worked for me on JellyBean
WebView webView = new WebView(getActivity(), null, android.R.attr.webViewStyle);
Sometimes the android.R.attr.webViewStyle is not applied by default. This ensures that the style is always applied.
For those of you looking for a solution when the WebView
is in an AlertDialog
, the answer here solved this problem for me: Show soft keyboard in AlertDialog with a WebView inside (Android)