Search code examples
androidadmob

Correctly disable AdMob ads


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);

Solution

  • 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.