Search code examples
javaandroidwebviewfavicon

Android WebView getFavicon() returning null


I'm trying to get the favicon of the loaded page after using

    WebView webView = new WebView(getActivity());
    webView.loadUrl("http://" + url);

I'm attaching the asynchronous WebViewClient to the WebView to get the favicon after it loads

    webView.setWebViewClient(new WebViewClient()
    {
        @Override
        public void onPageFinished(WebView view, String url)
        {
            String linkTitle = view.getTitle();
            Bitmap favicon = view.getFavicon();

            onLinkUrlFinished(url, linkTitle);
        }
    });

The favicon getting back is always null, even for websites such as google/facebook that has favicons for sure.

Another thread says to use WebIconDatabase but it's deprecated: Display the Android WebView's favicon

The API on android site refers to WebViewClient.onReceivedIcon which doesnt even exist.http://developer.android.com/reference/android/webkit/WebView.html#getFavicon%28%29

What's going on here?


Solution

  • In order to use onReceiveIcon(), you should use setWebChromeClient. This is what I do and it's working for me.

    webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                progressBar.setProgress(newProgress);
            }
    
            @Override
            public void onReceivedIcon(WebView view, Bitmap icon) {
                super.onReceivedIcon(view, icon);
                webImage.setImageBitmap(icon);
            }
        });