43

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);
Mark
  • 7,507
  • 12
  • 52
  • 88
  • 2
    Wait. You make money with Ads and you let the user disable them if they want to? Or do they need to pay to disable ads?? –  Dec 29 '10 at 10:53
  • 28
    I don't want to make money with the Ads. But I want to give the user the ability to support further app development by showing the ads. – Mark Dec 29 '10 at 11:26

9 Answers9

42

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.

Szymon
  • 42,577
  • 16
  • 96
  • 114
Quartertone
  • 654
  • 6
  • 9
  • @max4ever just add the view programmatically when you need to (or add a layout with only the adview and inflate it programmatically inside the viewgroup container). I'm guessing adding/removing shouldn't be called so many times, otherwise it's far simpler to just set the visibility to gone/visible. If it's only called a few times, then yes, add it programmatically, and take it off as suggested above. – usernotnull Mar 11 '17 at 00:31
  • I think you should remove the parentheses from (LinearLayout) adscontainer =... – Pablo Alfonso Mar 06 '20 at 14:58
  • Is this solution viable if I want to turn off all the ad for implementing in-app subscription purchase? – Ankit Sahu Aug 06 '22 at 12:34
27

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());
        }
    });
}
Chris Dryden
  • 271
  • 3
  • 2
  • 2
    Is visibility change enough? Setting the visibility GONE, still retains the view object alive. It is free to do ad requests, refreshes, etc. Removing view is better approach. – rpattabi Jul 22 '16 at 09:58
4

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.

Ognyan
  • 13,452
  • 5
  • 64
  • 82
3
  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.

Mohammad
  • 1,185
  • 1
  • 14
  • 26
2

Just setting the view's visibility to GONE is enough.

If you read the Logcat output, it prints "I/Ads: Ad is not visible. Not refreshing ad.".

zOqvxf
  • 1,469
  • 15
  • 18
1

Give it all you've got, just to be on the safe side:

if (mAdView != null) {
    mAdView.setEnabled(false);
    mAdView.setVisibility(View.GONE);

    ViewGroup parent = (ViewGroup) mAdView.getParent();
    if (parent != null) parent.removeView(mAdView);

    mAdView.removeAllViews();
    mAdView.destroy();
}
lenooh
  • 10,364
  • 5
  • 58
  • 49
0

Make a class which is called Ads like this

    public static boolean Show(AdView mAdView){

        if(mAdView != null){
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
            return true;
        }
        return false;
        }
    public static void Remove(AdView mAdView,ViewGroup parent){
        root.removeView(mAdView);
        }
    public static void Remove(AdView mAdView,RelativeLayout mLayout){
        mLayout.removeView(mAdView);
        }
    public static void Remove(AdView mAdView, LinearLayout mLayout){
        mLayout.removeView(mAdView);
        }
      }

Now import it in the class where you want to remove an AdView from use Ads.Remove(AdView, parent); to remove it. `Ads.Remove("the add view itself", the ViewGroup or layout where it is");

Ahmed I. Elsayed
  • 2,013
  • 2
  • 17
  • 30
0

setVisibility(VIEW.GONE); will remove the adview from the layout.

There might be away to fully remove it from the layout, but I've never had to do that.

HaMMeReD
  • 2,440
  • 22
  • 29
0

I'm not sure if the layout reflows after you remove the adview; if it doesn't you can create an identical (except for the missing ad) layout. In your OnCreate, select the proper layout. Then, you just need to ensure that a configuration change is triggered after the user makes the selection (may already happen depending on how you let them select - e.g. returning from a preference screen).

ProjectJourneyman
  • 3,566
  • 1
  • 27
  • 37