What's the difference between setWebViewClient vs. setWebChromeClient?
From the source code:
// Instance of WebViewClient that is the client callback.
private volatile WebViewClient mWebViewClient;
// Instance of WebChromeClient for handling all chrome functions.
private volatile WebChromeClient mWebChromeClient;
// SOME OTHER SUTFFF.......
/**
* Set the WebViewClient.
* @param client An implementation of WebViewClient.
*/
public void setWebViewClient(WebViewClient client) {
mWebViewClient = client;
}
/**
* Set the WebChromeClient.
* @param client An implementation of WebChromeClient.
*/
public void setWebChromeClient(WebChromeClient client) {
mWebChromeClient = client;
}
Using WebChromeClient allows you to handle Javascript dialogs, favicons, titles, and the progress. Take a look of this example: Adding alert() support to a WebView
At first glance, there are too many differences WebViewClient & WebChromeClient. But, basically: if you are developing a WebView that won't require too many features but rendering HTML, you can just use a WebViewClient
. On the other hand, if you want to (for instance) load the favicon of the page you are rendering, you should use a WebChromeClient
object and override the onReceivedIcon(WebView view, Bitmap icon)
.
Most of the times, if you don't want to worry about those things... you can just do this:
webView= (WebView) findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
And your WebView will (in theory) have all features implemented (as the android native browser).
Are WebViewClient and WebChromeClient mutually exclusive?
You certainly can use both, they just have different functions. Setting your own custom WebViewClient lets you handle onPageFinished, shouldOverrideUrlLoading, etc., WebChromeClient lets you handle Javascript's alert() and other functions.
Just make your own class, for example:
public class MyWebChromeClient extends WebChromeClient {
//Handle javascript alerts:
@Override
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
{
Log.d("alert", message);
Toast.makeText(context, message, 3000).show();
result.confirm();
return true;
};
...
and / or
public class MyWebViewClient extends WebViewClient {
@Override
//Run script on every page, similar to Greasemonkey:
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:alert('hi')");
}
...
Just override the functions described in the documentation, then set your client in onCreate with:
webview.setWebViewClient(new MyWebViewClient());
webview.setWebChromeClient(new MyWebChromeClient());
Android setWebChromeClient not working
Try to set custom WebViewClient
:
browser.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
});
WebChromeClient
let you handle JavaScript
functions.
WebViewClient
let you handle loading page (shouldOverrideUrlLoading()
) and complete loading page (onPageFinished()
).
Getting a webview to load with webchromeclient
Maybe because you are initializing the same webview twice and during the second time, you are not calling loadUrl() method on it.
Try setting the url after the second initialization also. The problem is that you are setting the content view to the webview, but the webview has no url to load.
How to connect webviewclient and Chromeweb view client
Replace your MyWebViewClient with this code :
private class MyWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
liProgressContainer.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
//return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
liProgressContainer.setVisibility(View.GONE);
//hide header part
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
view.loadUrl("file:///android_asset/error.html");
}
}
Related Topics
How to Exit When Back Button Is Pressed
Remove Application Icon and Title from Honeycomb Action Bar
How to Draw Arc Between Two Points on the Canvas
Does the Android Art Runtime Have the Same Method Limit Limitations as Dalvik
Android Studio/Emulator on MACos with Arm CPU M1
Detect Viewpager Tab Change Inside Fragment
Choosing Photo Using New Google Photos App Is Broken
Android Room Persistent Library - How to Insert Class That Has a List Object Field
Android Apps, Communicating with a Device Plugged in the Usb Port
How to Pair Bluetooth Device Programmatically Android
How Does One Check If a Table Exists in an Android SQLite Database
How to Resize Image (Bitmap) to a Given Size
Sending and Receiving Text Using Android Emulator
How to Send Objects Through Bundle
Google Maps API V2 Supportmapfragment Inside Scrollview - Users Cannot Scroll the Map Vertically