hakki
hakki

Reputation: 6501

How to open market:// links with WebView in Android

I'm using LeadBolt ads on my application (actually I use HTML style ads) on my content page. On bottom of page my ads shown.

Ads contains market links like market://. When I click banner It tries to open this link in web view so it fails. How can I open only market links out of WebView?

Only if ads link contains market:// it should open app detail page on market, else urls should open in WebView

String fullLink = "http://" + LINK; 

String html = "<html><body style='margin:0;padding:0;'>" +
"<iframe src='"+fullLink+"' style='position: absolute; border: none; box-sizing: border-box; width: 100%; height: 100%;'></iframe>"+
"<div style='z-index:99;position:absolute;bottom:0;'>"+
"<script type='text/javascript' src='http://ad.leadboltads.net/show_app_ad.js?section_id=123456789'></script></div></body></html> " ;

mWebView.loadData(html, "text/html", "utf-8");
mWebView.setWebViewClient(new HakkiWebViewClient());

and my custom web client is

//custom web client
private class HakkiWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {     
            view.loadUrl(url);
            return true;
        }
    }

Upvotes: 0

Views: 2242

Answers (2)

Himanshu itmca
Himanshu itmca

Reputation: 395

webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (Uri.parse(url).getScheme().equals("market")) {
                    try {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        Activity activity = (Activity) view.getContext();
                        activity.startActivity(intent);
                        return true;
                    } catch (ActivityNotFoundException e)
                    {
                        // Google Play app is not installed, you may want to open the app store link
                        // Link will open your browser
                        Uri uri = Uri.parse(url);
                        view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
                        return false;
                    }

                }
                return false;
            }
        });

Upvotes: 2

Nyx
Nyx

Reputation: 2243

You could do something like this:

protected Boolean checkUrl(String url) {
    if (url.contains("market://")) {
            Intent intent = new Intent("android.intent.action.VIEW");
            intent.setData(Uri.parse(url));
            startActivity(intent);
            return true;
    }
    return false;
}

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (checkUrl(url)) return true;
    view.loadUrl(url);
    return false;
}

Upvotes: 1

Related Questions