Jay Vyas
Jay Vyas

Reputation: 2712

Clickevents are not working in web view android

I have to create we application in android.

So what i done is that,simply created raw folder under res and put html files there.

All works fine but when i click a button that is put inside that web page nothing happens and the click event not get work.

Here are my codes.

newfile.html

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form>
<input type="button" value="Click me" onclick="openDialog()">
<script>
function openDialog()
{

alert("ok");
}
</script>
</form>
</body>
</html>

and this is my java code,

webview.loadData(readTextFromResource(R.raw.newfile),"text/html", "utf-8");

readTextFromResource function

    private String readTextFromResource(int resourceID)

{
    InputStream raw = getResources().openRawResource(resourceID);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    int i;
    try
    {
        i = raw.read();
        while (i != -1)
        {
           stream.write(i);
            i = raw.read();
        }
        raw.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
return stream.toString();
}

Please some one point me why the click event not working !

Upvotes: 2

Views: 17117

Answers (3)

Hazman
Hazman

Reputation: 1

I had this problem also. I fixed it by just set the WebChromeClient. Put this line at the onCreate method.

webView.setWebChromeClient(new WebChromeClient());

Upvotes: 0

CoolMind
CoolMind

Reputation: 28748

When a WebView doesn't respond to a button, you can test a web page inside a mobile browser or a desktop browser with minimum width (adaptive layout). You will see how the button works when you click it. Then you can press F12 and see what queries are sent in "Network" tab.

enter image description here

(If you press Ctrl+Shift+C, you will see a layout of the page, then can click an element (the button that you look for) and see a code.)

If the button shows a picture dialog (take a photo, upload a picture), you should use onShowFileChooser ([1]).

val startActivityForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult ->
    //
}

@SuppressLint("SetJavaScriptEnabled")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    with(webview) {
        settings.javaScriptEnabled = true
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.domStorageEnabled = true
        // webViewClient = MyWebViewClient()
        webChromeClient = MyWebChromeClient()
    }
}

private class MyWebChromeClient : WebChromeClient() {

    override fun onShowFileChooser(
        webView: WebView?,
        filePathCallback: ValueCallback<Array<Uri>>?,
        fileChooserParams: FileChooserParams?
    ): Boolean {
        // Check permissions, create intent.
        startActivityForResult.launch(chooserIntent)
        return true
    }
}

In case you have a JavaScript button, you can attach JavaScript interface ([1], [2], [3]):

@SuppressLint("SetJavaScriptEnabled")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    with(webview) {
        settings.javaScriptEnabled = true
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.domStorageEnabled = true
        // WebView.setWebContentsDebuggingEnabled(BuildConfig.TEST)
        addJavascriptInterface(
            JsHandle(this@WebViewFragment),
            "Android"
        )
    }
}

class JsHandle(private var fragment: WebViewFragment) {

    @JavascriptInterface
    fun notify(notify: String) {
        //
    }
}

Your web page should have javascript: inside.

Upvotes: 0

shalin
shalin

Reputation: 452

I am doing this type of things using phonegap. If you want to call native functions, use java script enabled web view. I am extends DroidGap in my MainActivity and my Login.html file under assets folder
in your Main Activity.java,

WebView webView=new WebView(this);
webview.loadUrl("file:///android_asset/www/Phone/Login.html");
setContentView(webView);

Anyway, if problem not solved, try adding this (all codes in onCreate method)

    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setSaveFormData(true);
    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setAllowFileAccessFromFileURLs(true);
    webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webView.getSettings().setSupportZoom(true);
    webView.setWebViewClient(new WebViewClient());
    webView.setClickable(true);
    webView.setWebChromeClient(new WebChromeClient());

If you want more about how to access Android native code through webpage here is the link

Upvotes: 10

Related Questions