Clickevents are not working in web view android

11,450

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

Share:
11,450
Jay Vyas
Author by

Jay Vyas

Updated on June 15, 2022

Comments

  • Jay Vyas
    Jay Vyas almost 2 years

    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 !