How do I disable Google Adsense in an WebView Android App?

Viewed 2113

I have a website with Google Adsense ads. I want to integrate this website into an Android application using a Webview. However, I understand that Google does not allow their ads to be placed in any app including inside a webview. (They recommend Admob instead)

Therefore, what is the best way to detect that my website is in a WebView and disable the Adsense ad accordingly? I want to keep the adsense ads for my desktop users, but disable them altogether for my mobile app users. Any ideas? Thanks.

1 Answers

You can execute javascript to hide different views including ads while integrating your website in web view. Below is a sample code -

public class MainActivity extends AppCompatActivity {

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView=(WebView) findViewById(R.id.web);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                String javaScript = "javascript:(function() { document.getElementById('google_image_div').remove();})()";

                webView.loadUrl(javaScript);    

            }
        });

        webView.loadUrl("YOUR_WEBSITE_URL");;

    }
}
Related