2

I'm trying to make an app that opens all web pages of a certain site, for example www.yahoo.com, in the webview, but all other web pages in the defaultbrowser. Here is the code that I am using but can not quite get it work.

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("http://www.yahoo.com")) {
            // This is my web site, so do not override; let my WebView load
            // the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch
        // another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

It looks to me that this code should work, but when i load yahoo it still goes to an outside browser. Any help would be appreciated. Thanks!

user2016462
  • 237
  • 1
  • 3
  • 6

3 Answers3

4

Here is a different way to do it.

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    String urlHost = Uri.parse(url).getHost();
    switch (urlHost) {
        case "yahoo.com":
            return false;
        case "www.yahoo.com":
            return false;
        case "m.yahoo.com":
            return false;
        default:
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
    }
}
dasmikko
  • 696
  • 2
  • 8
  • 29
1

getHost() usually returns just the domain name, or sometimes with www. prefixed to it. It never contains an http:// protocol AFAIK. Try using:

if ((Uri.parse(url).getHost().equals("yahoo.com")) ||  (Uri.parse(url).getHost().equals("www.yahoo.com")) ||  (Uri.parse(url).getHost().equals("m.yahoo.com"))) {
    //Your code
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Unfortunately, that code does not fix the problem. When yahoo.com is opened in the browser instead of the webview, the url is m.yahoo.com. Maybe this has something to do with it? – user2016462 Feb 10 '13 at 20:49
1

Try this it should help you

private WebView mWebview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yahoo);

        mWebview  = new WebView(this);

        mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

        final Activity activity = this;

        mWebview.setWebViewClient(new WebViewClient() {
            @SuppressWarnings("deprecation")
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
            @TargetApi(android.os.Build.VERSION_CODES.M)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
                // Redirect to deprecated method, so you can use it in all SDK versions
                onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
            }
        });

        mWebview .loadUrl("http://www.yahoo.com");
        setContentView(mWebview );

    }

}
Android
  • 1,420
  • 4
  • 13
  • 23
13Tracso
  • 479
  • 4
  • 8