Android webview Adblocker is a simple library to block ads in webview. this code is basicaly stop the ads and remove the html from it and render it again.
#1 Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency in app level build.gradle file
dependencies {
implementation 'com.github.MonsterTechnoGits:WebViewAdblock:1.2'
}
Inisialize the webview in your activity where you have the webview.
new AdBlockerWebView.init(this).initializeWebView(webView);
Now to block ads we need to add this code in the WebViewClient so extentd the WebViewClient for your webview like this and then in the class overide this method.
webView.setWebViewClient(new Browser_home());
private class Browser_home extends WebViewClient {
Browser_home() {}
@SuppressWarnings("deprecation")
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
return AdBlockerWebView.blockAds(view,url) ? AdBlocker.createEmptyResource() :
super.shouldInterceptRequest(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (AdBlocker.isAd(request.getUrl().toString())) {
// Block the ad by returning true
return true;
} else {
// Allow regular URLs to be loaded
return false;
}
}
}
Now you can run your app to see if the ads are blocked. You can always use other webview features and add fetures to the webview.