Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correctly disable AdMob ads

Tags:

android

admob

I am integrating AdMob into my app and I wonder how to disable Ads correctly. I want to give the user the ability to disable them. I don't want to get any problems with AdMob because of wrong impressions. Is this way correct or should I consider something?

AdView ads = (AdView) findViewById(R.id.ad);
ads.setEnabled(false);
like image 776
Mark Avatar asked Dec 28 '10 20:12

Mark


People also ask

How do I turn off ads?

You can block ads on your Android phone by opening Chrome and tapping on the three-dot menu button. Go to Settings > Site settings > Ads and turn the toggle on.


4 Answers

In your layout file (eg, main.xml) :

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/adsContainer">

    <com.admob.android.ads.AdView 
        android:id="@+id/admobAds" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        app:backgroundColor="#000000" 
        app:primaryTextColor="#FFFFFF" 
        app:secondaryTextColor="#CCCCCC">

</LinearLayout>

Then in your code (eg, inside a "if" block)

(LinearLayout) adscontainer = (LinearLayout) findViewById(R.id.adsContainer);

View admobAds = (View) findViewById(R.id.admobAds);

adscontainer.removeView(admobAds);

This will "permanently" (for the lifecycle of the app) remove the ads from the layou, which means that there will not be any ads requested.

like image 81
Quartertone Avatar answered Oct 28 '22 23:10

Quartertone


I also wanted to give users the ability to disable ads - why force people to see them if they don't want to? and why should you expect people to pay for that option?

Anyway, I outlined how I did this in this article. The article goes into more detail but here's the relevant parts:

The code i use to turn off ads:

private void hideAd() {
    final AdView adLayout = (AdView) findViewById(R.id.adView1);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            adLayout.setEnabled(false);
            adLayout.setVisibility(View.GONE);
        }
    });
}

And to turn them back on (in case anyone was feeling generous)

private void showAd() {
    final AdView adLayout = (AdView) findViewById(R.id.adView1);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            adLayout.setEnabled(true);
            adLayout.setVisibility(View.VISIBLE);
            adLayout.loadAd(new AdRequest());
        }
    });
}
like image 31
Chris Dryden Avatar answered Oct 28 '22 22:10

Chris Dryden


Unfortunately the setVisibility(View.GONE); + setEnabled(false) combo does not work universally on all android versions / devices. Depending on when do you invoke it you may end up hanged in empty screen (no NPE, just blank screen).

So far the only solution that works for me is:

For Activity:

protected void removeAdView(int adViewId) {
    View view = getWindow().getDecorView();
    View adView = view.findViewById(adViewId);

    if (adView != null) {
        ViewGroup parent = (ViewGroup) adView.getParent();
        parent.removeView(adView);
        parent.invalidate();
    }
}

For Fragment:

protected void removeAdView(View topView, int adViewId) {
    View adView = topView.findViewById(adViewId);

    if (adView != null) {
        ViewGroup parent = (ViewGroup) adView.getParent();
        parent.removeView(adView);
        parent.invalidate();
    }
}

This solution is based on @Quartertone's answer but extended to be more universal (i.e. works with all ViewGroups not just LinearLayout). Just put these methods in your base Activity/Fragment classes.

like image 4
Ognyan Avatar answered Oct 28 '22 23:10

Ognyan


  1. Create a new class derived from AdView;

    package com.MyApp;
    
    import android.app.Activity;
    import android.content.Context;
    import android.util.AttributeSet;
    
    import com.google.ads.AdRequest;
    import com.google.ads.AdSize;
    
    public class MyAdView extends com.google.ads.AdView {
    
    public MyAdView(Activity activity, AdSize adSize, String adUnitId) {
        super(activity, adSize, adUnitId);
        if (MyApp.m_ads_enabled) {
            AdRequest adRequest = new AdRequest();
               loadAd(adRequest);
        }
    }
    
    public MyAdView(Context context, AttributeSet attrs) 
    {
        super(context, attrs); 
        if (MyApp.m_ads_enabled) {
            AdRequest adRequest = new AdRequest();
               loadAd(adRequest);
        }
    }
    
    MyAdView(Context context, AttributeSet attrs, int defStyle) 
    {
        super(context, attrs, defStyle);
        if (MyApp.m_ads_enabled) {
            AdRequest adRequest = new AdRequest();
               loadAd(adRequest);
        }
    }
    
  2. in your XML define your advert using MyAdView rather than the regular AdView and set the loadAdOnCreate attribute to false, e.g.;

<com.MyApp.MyAdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="0"
                         ads:adSize="BANNER"
                         ads:loadAdOnCreate="false"/>

Then, depending on the value of MyApp.m_ads_enabled when you call setContentView() the ads will either be disabled or enabled.

This approach has the advantage that, with ads disabled, no data bandwidth will be used as the ad never gets requested, this may be important to someone on a limited or PAYG data contract.

like image 3
Steve Monks Avatar answered Oct 28 '22 22:10

Steve Monks