0

I have a webview that loads in a fragment in Android. When the user taps a link on the webpage loaded in the webview, I want the new page to be loaded in the default browser on Android outside of my app. Any ideas how to do this? Thanks!

What I have so far:

FrameLayout layout = new FrameLayout(context);

WebView webView = new WebView(getActivity());
webView.setBackgroundColor(Color.TRANSPARENT);
webView.loadUrl(InvestBetterURL);
webView.setWebViewClient(new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){
      return false;
    }
    @Override
    public void onPageStarted (WebView view, String url, Bitmap favicon)
    {
        view.setVisibility(View.GONE);
    }

    @Override
    public void onPageFinished (WebView view, String url)
    {
        view.setVisibility(View.VISIBLE);
    }

});

layout.addView(webView);
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
John
  • 250
  • 3
  • 15

1 Answers1

5

Change the shoudlOverrideUrlLoading to true. Check the link: http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)

UPDATED:

private class CustomWebViewClient extends WebViewClient {
        @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
              if(url.contains("google.com")) {
                view.loadUrl(url);
              } else {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(i);
              }
              return true;
            }
        }
user2511882
  • 9,022
  • 10
  • 51
  • 59
  • Thanks for the response! I don't want it to load externally unless the user tries to navigate away from the URL already opened in the WebView. Doesn't this make it always load the URL externally? – John Dec 16 '13 at 18:54
  • Yes. I misunderstood the question. You will need to send an intent based on the url. I will update my answer – user2511882 Dec 16 '13 at 19:13
  • Please accept and close the question if it helped you.Thanks – user2511882 Dec 16 '13 at 19:17