1

I'm loading a url into a webview and accessing the "onPageFinished" method to hide the loading spinner. However, this seems to block all phone number links and street address links from clicking through. They will appear as links but nothing happens when clicking them.

If I comment out the "mWebView.setWebViewClient ..." area below, then phone number links will bring up the dialer and street addresses will be mapped on Google Maps, but the spinner keeps on spinning in the title bar after the page loads.

Any ideas? thanks!

The xml:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

And the java:

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);

    Map <String, String> headerAuth = new HashMap <String, String>();
    headerAuth.put("Authorization", "Basic " + encodedString);
    String url = "URL to my web page";

    mWebView.setWebViewClient(new WebViewClient() {
           public void onPageFinished(WebView mWebView, String url) {
               g_progressBar.setVisibility(View.INVISIBLE);
           }
    }); // commenting this out fixes problem, but I need it to remove loading indicator in title bar

    mWebView.loadUrl(url, headerAuth);
TrippinBilly
  • 957
  • 2
  • 10
  • 19

2 Answers2

2

I figured it out by piecing together two StackOverFlow posts:

Has most of the code

contains the geo: part

mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.startsWith("tel:")) {
                    startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
                } else if (url.startsWith("mailto:")) {
                    url = url.replaceFirst("mailto:", "");
                    url = url.trim();
                    Intent i = new Intent(Intent.ACTION_SEND);
                    i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL, new String[]{url});
                    startActivity(i);
                } else if (url.startsWith("geo:")) {
                    Intent searchAddress = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
                    startActivity(searchAddress); 
                } else {
                    view.loadUrl(url);
                }
                return true;

            }

            @Override
            public void onPageFinished(WebView mWebView, String url) {
                super.onPageFinished(mWebView, url);
                g_progressBar.setVisibility(View.INVISIBLE);
            }

    });
Community
  • 1
  • 1
TrippinBilly
  • 957
  • 2
  • 10
  • 19
0

Just call the method of the original WebViewClient :

public void onPageFinished(WebView mWebView, String url) {
    g_progressBar.setVisibility(View.INVISIBLE);
    super.onPageFinished(mWebView, url);
}

It doesn't work, so you need to override shouldOverrideUrlLoading() in your class WebViewClient. Here is what you can do :

new WebViewClient() {
@Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (DEBUG && ConfigApp.DEBUG) {
                Log.d(TAG, "shouldOverrideUrlLoading " + url);
            }

            boolean returnValue = false;

            // Exemple : if(url.startsWith("http://") => If the link is for 
            // a webpage, handle it !

            if (whatyouwanttobeclickable) {
                returnValue = true;
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(i);
            }

            return returnValue;
        }

        public void onPageFinish() {
            //your code
        }
}
NitroG42
  • 5,336
  • 2
  • 28
  • 32
  • Unfortunately, the same undesired behavior is experienced -- phone number and street address links are displayed but do not click through. – TrippinBilly Aug 11 '11 at 16:25
  • @TrippinBilly I have updated my answer with something that will work. – NitroG42 Aug 11 '11 at 21:09
  • @TrippinBilly, sorry some words was missing ! – NitroG42 Aug 12 '11 at 07:04
  • this doesn't seem to work either. If I had to override another class method, it would probably be onLoadResource() since I'm looking for geo: and tel: resources being accessed. I'm not sure why I'd have to override these anyway since I want them to behave as normal. But the overriding doesn't make sense. Even if I just override onPageFinish() and only call super.onPageFinished(mWebView, url) --- I still hit the issue. – TrippinBilly Aug 16 '11 at 15:14
  • Disregard the reference to onLoadResource(). You are correct that shouldOverrideUrlLoading() handles the tel: and geo: :-) The rest of the comment is still valid though, as I'm still hitting the issue – TrippinBilly Aug 16 '11 at 15:24
  • The following code, just putting in the default shouldOverrideUrlLoading method that's contained in the Android source code, will still produce the error I'm seeing :-~ mWebView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } }); – TrippinBilly Aug 16 '11 at 16:12