I want to block ad from a website I am loading in an android studio webview. Say I am browsing a website in google chrome the website then showing the ad containing on the website. When I am loading that website inside my webview normally the ad is shown. I want to block that ad via the modification of java code. I have tried a method of GitHub. It's not working. Can I get the easiest and well-documented solution for it?
I try
build.gradle
implementation 'org.adblockplus:adblock-android:3.0'
XML
<org.adblockplus.libadblockplus.android.webview.AdblockWebView
android:id="@+id/main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Java
WebView webView;
@SuppressLint("SetJavaScriptEnabled")
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.main_webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("url"); }
or I have another idea of injecting additional CSS to solve this,
Idea 2:
If I can inject an addition CSS file "style2.css
" from assets/style2.css
of an android studio. This may work. I can find out the div class inside that the ad is shown.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="row">
<div class="google-ad">
***Google Ad Banner***
</div>
</div>
<div class="header">
***Webpage Content Here****
</div>
</body>
</html>
say class "row
" contains an ad. I will then call .row
class and make it *display: none*
. But how can I inject an additional CSS file to a webpage what is loaded to my webview?
Let me clear my idea2 again with another example,
Say I am browsing "www.google.com" website. This google webpage has a white background. But I want to make the white background to black when I will load it to my android studio webview. By calling or injecting another CSS file from assets/cssbg.css.
cssbg.css
code will be
body{
background: #000000 !important;
}
I have already reviewed this, Android block ads in webview
but the problem is it is difficult to understand say this portion, public class MyWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) { }
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".mp4")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "video/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(intent);
return true;
} else if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:")
|| url.startsWith("mms:") || url.startsWith("mmsto:")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(intent);
return true;
} else {
return super.shouldOverrideUrlLoading(view, url);
}
}
why .mp4
?
I just want to know the whole process in a simpler way, will you please cooperate?