Tapping form field in WebView does not show soft keyboard

AndroidWebview

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

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

Solution 4 - Android

AndroidChen's answer did not work for me at all, but I searched the link provided (http://code.google.com/p/android/issues/detail?id=7189) and I found the following class, it works perfectly (Android Froyo on HTC Bravo), not just text, but also all buttons, redirects, etc, everything works perfectly :D

class LiveWebView extends WebView
{
	Context mContext;
	
	public LiveWebView(Context context, String URL)
	{
		super(context);
		mContext = context;
		setWebViewClient(URL);
	}
	
	@Override
	public boolean onCheckIsTextEditor()
	{
		return true;
	}
	
	@SuppressLint("SetJavaScriptEnabled")
	boolean setWebViewClient(String URL)
	{
		setScrollBarStyle(SCROLLBARS_INSIDE_OVERLAY);
		setFocusable(true);
		setFocusableInTouchMode(true);
		requestFocus(View.FOCUS_DOWN);
		
		WebSettings webSettings = getSettings();
		webSettings.setSavePassword(false);
		webSettings.setSaveFormData(false);
		webSettings.setJavaScriptEnabled(true);
		webSettings.setSupportZoom(false);
		webSettings.setUseWideViewPort(false);
		
		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;
			}
		});
		
		this.setWebViewClient(new WebViewClient()
		{
			ProgressDialog dialog = new ProgressDialog(mContext);
			
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url)
			{
				loadUrl(url);
				
				return true;
			}
			
			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
			{
				Toast.makeText(mContext, "Oh no! " + description, Toast.LENGTH_SHORT).show();
			}
			
			public void onPageStarted(WebView view, String url, Bitmap favicon)
			{
				if (dialog != null)
				{
					dialog.setMessage("Loading...");
					dialog.setIndeterminate(true);
					dialog.setCancelable(true);
					dialog.show();
				}
				
			}
			
			public void onPageFinished(WebView view, String url)
			{
				if (dialog != null)
				{
					dialog.cancel();
				}
			}
		});
		
		this.setWebChromeClient(new WebChromeClient()
		{
			public void onProgressChanged(WebView view, int progress)
			{
				// Activities and WebViews measure progress with different scales.
				// The progress meter will automatically disappear when we reach 100%
			}
			
			@Override
			public boolean onJsAlert(WebView view, String url, String message, JsResult result)
			{
				result.confirm();
				return true;
			}
		});
		
		loadUrl(URL);
		
		return true;
	}
}

and then to create a webview within a dialog, I wrote this code

AlertDialog.Builder alert = new AlertDialog.Builder(M_PaymentOptions.this);
alert.setNegativeButton("Back to Payment Options", new DialogInterface.OnClickListener()
{
	@Override
	public void onClick(DialogInterface dialog, int id)
	{}
});

alert.setTitle("BarclayCard Payment");

LiveWebView liveWevViewObject = new LiveWebView(M_PaymentOptions.this, redirecturl);

alert.setView(liveWevViewObject);

alert.show();

Solution 5 - Android

Just add invisible EditText under WebView

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible" />

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

All these focus-related solutions didn't work on my Samsung Note 3/Android 5.0

Solution 6 - Android

As mentioned here on Google Issue Tracker this fix works fine. Nothing else needed.

webView.isFocusable = true
webView.isFocusableInTouchMode = true
webView.requestFocus(View.FOCUS_DOWN)

Solution 7 - Android

For anyone who is having this in a DialogFragment (or maybe a Dialog in general), this may be related to this bug: https://issuetracker.google.com/issues/36915710

Here's the workaround which worked for me:

https://stackoverflow.com/a/16573061/1316677

Summary: create an invisible/"gone" EditText in the layout in the Dialog, then call:

     EditText edit = view.findViewById(R.id.editText);
     edit.setFocusable(true);
     edit.requestFocus();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShawn LauzonView Question on Stackoverflow
Solution 1 - AndroidandroidchenView Answer on Stackoverflow
Solution 2 - AndroidSteveView Answer on Stackoverflow
Solution 3 - AndroidMrad4TechView Answer on Stackoverflow
Solution 4 - AndroidMohamed HeibaView Answer on Stackoverflow
Solution 5 - AndroidHomo IncognitoView Answer on Stackoverflow
Solution 6 - AndroidDanielView Answer on Stackoverflow
Solution 7 - AndroidOdedView Answer on Stackoverflow