0

i have a webview where i load an url and when i click the webview i want to open the link in the default browser.I tried to set the WebViewClient and override shouldOverrideUrlLoading(WebView view, String url) method

 @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.e("BaseFragment", "shouldOverrideUrlLoading " + url); Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(i); return false; } 

but it is not called by the webview. Any other ideas to do this?

BaseFragment is an abstract class where i have the following WebViewClient:

protected WebViewClient mAddWebViewClient = new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { float scale = addTopWebView.getHeight() / 170f; addTopWebView.setInitialScale((int) (scale * 100f)); String js = "javascript:(function(){" + "document.getElementsByTagName('div')[0].style.height = 'auto';" + "document.getElementsByTagName('div')[0].style.width = 'auto';" + "})()"; view.loadUrl(js); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.e("BaseFragment", "shouldOverrideUrlLoading " + url); Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(i); return true; } }; 

And other fragments which extend this BaseFragment have some webviews and there in onViewCreated method i set the webviewclient:

 myWebView.getSettings().setJavaScriptEnabled(true); addTopWebView = myWebView; myWebView.setWebViewClient(mAddWebViewClient); 
1
  • 1
    i don't see anything wrong here (you probably will want to return true to avoid loading the page also in the webview, but that is not relevant for now). Can you post more code? (creating the webview, setting the WebViewClient...) Commented Nov 4, 2014 at 15:37

1 Answer 1

2

I think you should return true for the external browser.

@Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.e("BaseFragment", "shouldOverrideUrlLoading " + url); Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(i); return true; } 

If you want to show in a custom webview you shoud override and set the return to false.

Sign up to request clarification or add additional context in comments.

3 Comments

even if i return true it does nothing..the method is not called
Could you give me more details on how your code works? You want to open a web browser from a fragment? Or could you post a larger chunk of code please?
i added more explanation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.