16

In my Android app I create an AlertDialog that has a WebView inside. The WebView loads a webpage that requires the user to log in. However, when I click on text fields in the WebView, soft keyboard does not appear. I am aware of the issue in general (Android: Issue 7189); however, in my case the suggested solution does not seem to work since I use an external website, and not just a simple HTML form.

The perfect solution would be if the keyboard appeared when the user clicked on the website's text fields. However, having the keyboard appear together with theAlertDialog would also work. Any ideas?

3 Answers3

26

It seems that the best solution is to simply create a custom dialog. Custom dialogs do not appear to have the soft keyboard bug at all (it shows up exactly when it has to). Here's some basic code:

Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("My great title");
dialog.setCancelable(true);

dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);

WebView vw = (WebView) dialog.findViewById(R.id.wv);
datu-puti
  • 1,306
  • 14
  • 33
  • 2
    please share your complete code. I am having same problem but I am not loading my webview in dialog. I am launching it in an Activity and pressing on any text field is not showing keyboard. help me – AZ_ Jun 01 '11 at 15:42
  • 1
    Thanks!!! for me the AlertDialog was the issue, when changed to Dialog, it works!! tried all possible workarounds with AlertDialog, focus, viewport, touch listeners, custom webview classes etc... – Arun Jose Oct 27 '14 at 08:47
  • @AZ_ if you're not using a dialog, how is it the same problem? – eis Mar 06 '18 at 12:22
  • 1
    @eis This bug has been fixed by Google. It's 6 years old post so I don't even have slight idea :p – AZ_ Mar 08 '18 at 15:10
5

There much better solution (with AlertDialog).

  1. Extend WebView class.

    public static class LocalWebView extends WebView {
        public LocalWebView(Context context) {
            super(context);
        }
    
        public LocalWebView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public LocalWebView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public LocalWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
       }
    
        public LocalWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
            super(context, attrs, defStyleAttr, privateBrowsing);
        }
    
        @Override
        public boolean onCheckIsTextEditor() {
            return true;
        }
    }
    
  2. Use this LocalWebView instead of origin WebView in layout that you set to AlertDialog as content view.

humanoid
  • 141
  • 1
  • 5
  • 4
    Thanks, it works. You can also just use diectly: `new WebView(this){ @Override public boolean onCheckIsTextEditor() { return true; } };` – crgarridos Nov 14 '17 at 10:06
  • 6 years later and apparently this is still an issue with WebView.. no keyboard when clicking on an input field. Extending onCheckIsTextEditor() fixed it for me too – jpage4500 Feb 23 '23 at 21:59
0

If it's still relevant, for me the problem was related to an immersive dialog + webview.

I've override the dialog show method to achieve immersive by setting a flag - NOT FOCUS on the dialog window, once I removed it I lost the immersivnes but now my web dialog pops uo the keyboard...

user3193413
  • 575
  • 7
  • 10