Promes
Promes

Reputation: 1

Android webview open external links in own application

When external links are clicked on my website, I want them to open in the application it is connected to. But I am getting page not found error. For example, when instagram.com is clicked, it will open on Instagram.

Upvotes: 0

Views: 167

Answers (1)

mohit48
mohit48

Reputation: 812

You need to define a webView CLient like this:

private class MyWebViewClient extends WebViewClient {
    //link opener
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if ("yourHost.com".equals(Uri.parse(url).getHost()))
        {
           
            // This is my website, so do not override; let my WebView load the page
            //loading.show();
            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;
    }
}

on set this webViewClient in your webView like: mywebView.setWebViewClient(new MyWebViewClient())

Upvotes: 1

Related Questions