Karthi
Karthi

Reputation: 13752

Alert is not appear from web view in android?

I use the sample provided by Google for demonstrating two way communication between JavaScript and Java ,

ref[1]:

http://code.google.com/p/apps-for-android/source/browse/trunk/Samples/WebViewDemo/src/com/google/android/webviewdemo/WebViewDemo.java

The functionality is working fine. I am able to call JavaScript function from Java and callback the Java function from JavaScript.

The problem is when I use an alert inside a JavaScript function it won`t appear, but the functionality inside the function is working properly.

Why does alert("test") inside a JavaScript function not appear in Android. I load the JavaScript in a web-view. When I a clicking button in Android I call the function, but it does not appear.

If anyone knows the problem, pealse help me out.

Thanks

Upvotes: 10

Views: 18350

Answers (4)

Md Nawaz
Md Nawaz

Reputation: 71

By adding the following two lines, my JavaScript works:

mWebview.setWebViewClient(new WebViewClient()());
mWebview.setWebChromeClient(new WebChromeClient());

Upvotes: 1

Chandan kushwaha
Chandan kushwaha

Reputation: 951

Just use WebChromeClient. It will do everything.

mWebview.setWebChromeClient(new WebChromeClient());

It will work.

Upvotes: 0

Rohith Murali
Rohith Murali

Reputation: 5669

Use the following mehtod,

WebView wv=new WebView(this);   
wv.setWebChromeClient(new WebChromeClient() {
@Override
    public boolean onJsAlert(WebView view, String url, String message,JsResult result) {
    //Required functionality here
    return super.onJsAlert(view, url, message, result);
}

Upvotes: 0

Android
Android

Reputation: 9023

setContentView(R.layout.main);
        WebView webview = (WebView) findViewById(R.id.webview);

        WebSettings webSettings = webview.getSettings();

        webSettings.setJavaScriptEnabled(true);

        webSettings.setBuiltInZoomControls(true);

        webview.requestFocusFromTouch();

        webview.setWebViewClient(new WebViewClient());
        webview.setWebChromeClient(new WebChromeClient());    

     webview.loadUrl("file:///android_asset/test.html");

this code working perfect and shows me alert box.. and this is my
test.html

<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("Hello! I am an alert box!");
}
</script>
</head>
<body>

<input type="button" onclick="show_alert()" value="Show alert box" />

</body>
</html>

Upvotes: 20

Related Questions