Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- 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.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement