Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android:How to add support the javascript alert box in WebViewClient?

Tags:

android

I implement the many things using the webViewClient like onUnhandledKeyEvent,shouldOverrideUrlLoading and more.If want to add the support for alertbox then need to switch to WebChromeClient then i can not do other things.Any one know how mix the both future?
I have check the code for javasript alert box at http://lexandera.com/2009/01/adding-alert-support-to-a-webview/


Thank you

like image 746
Sameer Z. Avatar asked Feb 07 '11 11:02

Sameer Z.


People also ask

How to enable JavaScript in WebView in Android?

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

How to Add WebView in Android?

Modify src/MainActivity. java file to add WebView code. Run the application and choose a running android device and install the application on it and verify the results. Following is the content of the modified main activity file src/MainActivity.

How WebView works?

Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.


1 Answers

You need to set WebChromeClient(This handles JavaScript dialogs, favicons, titles, and the progress) for your WebView .

WebView wView = new WebView(this){
            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                    JsResult result) {
                // TODO Auto-generated method stub
                Log.i("my log","Alert box popped");
                return super.onJsAlert(view, url, message, result);
            }
};
setContentView(wView);

wView.getSettings().setJavaScriptEnabled(true);

WebChromeClient cClient = new WebChromeClient();
wView.setWebChromeClient(cClient);

wView.loadUrl("file:///android_asset/" + yourHtmlFile);

Hope it helps :)

like image 121
Suryavel TR Avatar answered Oct 20 '22 11:10

Suryavel TR