GB Golden
GB Golden

Reputation: 43

How can i block or avoid popup in webview?

the webpage have some popup ads is there any way to prevent the popup from loading when the popup loads the main site doesnt appears is there any way to load the main page with out popups and how can i add a download handler l mean the webview should support downloading .torrent file

public class MainActivity extends AppCompatActivity {
    private WebView webView;
    private ProgressBar progressBar;
    private LinearLayout layoutProgress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        layoutProgress = (LinearLayout) findViewById(R.id.layoutProgress);
        webView.setVisibility(View.GONE);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
        settings.setDisplayZoomControls(false);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                webView.setVisibility(View.VISIBLE);
                layoutProgress.setVisibility(View.GONE);
                progressBar.setIndeterminate(false);
                super.onPageFinished(view, url);

            }
            public void but(View v){

            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                layoutProgress.setVisibility(View.VISIBLE);
                progressBar.setIndeterminate(true);
                super.onPageStarted(view, url, favicon);
            }
        });
        if(isOnline()) {
            webView.loadUrl("http://testsite.com/");
        } else {
            String summary = "<html><body><font color='red'>No Internet Connection</font></body></html>";
            webView.loadData(summary, "text/html", null);
            toast("No Internet Connection.");
        }
    }
    private void toast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    private boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        return (netInfo != null && netInfo.isConnected());
    }
    public void but(View v){

        webView.loadUrl("http://testsite.com/");
    }
}

Upvotes: 1

Views: 3389

Answers (2)

Matthew Shearer
Matthew Shearer

Reputation: 2795

if the url changes then use shouldOverrideUrlLoading with some regex

so

List<String> validUrls = new LinkedList<>();
validUrls.add("https://www\\.google\\.com/*");
validUrls.add("https://www\\.facebook\\.com/*");


@Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (isValidUrl(url)) {
        return false;
    }
    return true;
}

private boolean isValidUrl(String url) {
    for (String validUrl : validUrls) {
        Pattern pattern = Pattern.compile(validUrl, Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            return true;
        }
    }
    return false;
}

would match against any www.google.com or facebook.com urls

Upvotes: 1

You can intercept the urls that are opened from the webview, I don't know if it would work with the popup:

    WebViewClient client= new WebViewClient(){
        @Override 
        public boolean shouldOverrideUrlLoading(WebView  view, String  url){
           if (url.equals("popupURL"){ 
                return true; 
           }
            return false;
        } 
    } 

webView.setWebViewClient(client);

Upvotes: 0

Related Questions