Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 899
Jay Vyas Avatar asked Jan 24 '26 02:01

Jay Vyas


1 Answers

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

like image 90
shalin Avatar answered Jan 25 '26 14:01

shalin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!