79

From this great explanation about the differences between WebViewClient and WebChromeClient it seems that if you use one, you shouldn't be using the other (for the same WebView object).

Is my understanding correct?

If not, when would one use both WebViewClient and WebChromeClient for the same WebView object?

Is there an example of a situation where only use both WebViewClient and WebChromeClient for the same WebView object would accomplish a certain goal?

2 Answers 2

105

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());
6
  • I really meant an example of a situation, not code. :) Accepting unless a better answer comes along.
    – ef2011
    Commented Jun 29, 2011 at 18:16
  • 42
    @ef2011: Say u want to load a page containing a video into your webview. BUT the page is secured by an htaccess file. So,to authenticate u need to use setWebViewClient and overide its onReceivedHttpAuthRequest() method with ur credentials. Then u want to play the video.... but turns out the video doesnt play! Maybe its controlled via JS. Or it needs a plugin. So, in order to make full use of the content and better JS support you ALSO need to setWebChromeClient(). The way I see it: setWebViewClient for basic HTTP browser stuff, setWebChromeClient for content-related operations and support. Commented Feb 8, 2013 at 11:09
  • 2
    I agree with @ef2011. This comment is more clarifying than the code example above. Commented Nov 7, 2014 at 1:50
  • Maybe is a bit late, but I've provided another use case. :) Commented Jul 8, 2016 at 8:13
  • 1
    I wonder why the Android team not document these things in a simple, clear and understandable way. Commented Apr 21, 2020 at 20:47
11

You can use both clients simultaneously. It is useful as both of them provides different functionalities.

For example, if you would like to:

  • follow redirects in web view rather than opening available browser

and

  • trace the loading progress

which are impossible with just one of the clients you can do the following:

WebView webView = (WebView) findViewById(R.id.web_view);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        super.onProgressChanged(view, newProgress);

        // Your custom code.
    }
});

As the default implementation of shouldOverrideUrlLoading(WebView, String) method can be use as-is for above mentioned case - there is no need to override it unless you would like other behavior.

1
  • I really thought you cannot combine webviewclient and webchromeclient. I was having problem with location.href working only on webviewclient and failed to work if I use webchromeclient. thanks for this man
    – Jeo
    Commented Nov 17, 2022 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.