0

I am using WebView in my page and using local file assets to displaying in WebView but in main HTML page external website (not local) and I want to open just that link in default Browser on the users device this picture disciple situation

This is my code in 'onCreate' method

WebView v;

v=(WebView) rootView.findViewById(R.id.webView1);
v.getSettings().setJavaScriptEnabled(true);
WebViewClient vc= new WebViewClient();
v.setWebViewClient(vc);
v.loadUrl("file:///android_asset/home.html");

When I run the application the internal link is working good but the external link "www.apple.com" en in the web view

I searched the same question and found this solution but still external link opens in WebView

WebView webView = (WebView) rootView.findViewById(R.id.webView1);
webView.setWebViewClient(new MyWebViewClient());
String url = "file:///android_asset/home.html";
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);

and class

class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.contains("http")){ // Could be cleverer and use a regex
            return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
        } else {
            view.loadUrl(url); // Stay within this webview and load url
            return true;
        }
    }
}

1 Answer 1

3

Change

  if (url.contains("http")) { // Could be cleverer and use a regex
    return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
  } else {
    view.loadUrl(url); // Stay within this webview and load url
    return true;
  }

to

 if (url.contains("http")) { // Could be cleverer and use a regex
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    mContext.startActivity(intent);
    return true;
 }
 return false;

Note : replace mContext with your activity context.

1
  • 1
    @Milaaaad Happy to help,enjoy.
    – Giru Bhai
    Jun 19, 2015 at 9:30

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.