Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open ads in external browser in webview android

Tags:

webview

I created app with webview, and i want load all internal links in webview and load external links in android browser. Now problem is I am using html ads and when i click on ads i want open external browser, but its opening in webview. only problem with ads otherwise everything is works fine. So how can i do this?

My code is below:

`class MyWebViewClient extends WebViewClient {

@Override   
public boolean shouldOverrideUrlLoading(WebView view, String url) { if (Uri.parse(url).getHost().equals("www.mysite.com")) {
    view.loadUrl(url);
    return true;
}else{

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;}}`
like image 930
MacAnony Avatar asked Feb 02 '26 12:02

MacAnony


1 Answers

You code should be:

@Override   
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (Uri.parse(url).getHost().equals("www.mysite.com")) {
        return true;
    }else{
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return false;
    }
}

All I changed was:

1.) Returning true loads the URL in the webview, no need for view.loadUrl()

2.) Return false when you broadcast the ACTION_VIEW intent

like image 196
Matt Gaunt Avatar answered Feb 05 '26 07:02

Matt Gaunt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!