I'm making an app which runs in android webview. I'm facing a strange issue,
My JavaScript function is not getting called on <a></a>
onClick method.
Here is my html and JavaScript code:
<html>
<script type="text/javascript" src="http://blue.xxxxxxxx.com/xxxxx/site/js/jquery.js"></script>
<script>
function openFileDialog()
{
//$("#file").click();
alert("Test");
}
</script>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
<a href="javascript:;" onclick="openFileDialog();">Shujaat</a>
</body>
</html>
and here is my whole java webviews code.
package com.example.findozerapp;
import my.functions.MyFunctions;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
Context context = MainActivity.this;
Activity activity = MainActivity.this;
WebView webView;
MyFunctions myFunctions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFunctions = new MyFunctions(activity);
webView = (WebView) findViewById(R.id.webView1);
configureWebview();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
webView.loadUrl("http://www.xxxxxxx.com/qadir/");
}
private void configureWebview() {
webView.setPadding(0, 0, 0, 0);
webView.setInitialScale(myFunctions.setWebViewScale());
webView.getSettings().setDefaultZoom(ZoomDensity.FAR);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setCacheMode(MODE_APPEND);
webView.setWebViewClient(new MyWebViewClient());
}
private class MyWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
}
Notice I have enable javascript in my webview.
but its not calling my javascript function openFileDialog()
.
and one thing more. Whenever I load my this webpage in default android browser its working perfectly. Where i have did wrong. Please check my webview's settings.
If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With