15

I'm trying to set adUnitId programmatically to ads from the new Google Play services (old AdMob).

I have this in XML (used in an <include>):

<com.google.android.gms.ads.AdView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"/>

and this in onCreate():

AdView mAdview = (AdView)findViewById(R.id.adView);
    mAdview.setAdUnitId(((App)getApplication()).getAdmobKey());

    mAdview.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            findViewById(R.id.adView).setVisibility(View.VISIBLE);
        }
    });

    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    mAdview.loadAd(adRequest);

And I get:

The ad size and ad unit ID must be set before loadAd is called.

So the second option was to make the ad programmatically.

The new XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/adView"
    />

The new code:

AdView mAdview = new AdView(this);
...
((LinearLayout)findViewById(R.id.adView)).addView(mAdview);
mAdview.loadAd(adRequest);

But I get the same error.

I tried also to inherit from com.google.android.gms.ads.AdView to make a custom view, but it's final.

Any suggestion?

5

2 Answers 2

26

The method loadAd() checks if (mAdView.getAdSize() == null || mAdView.getAdUnitId() == null) when loadAd happens.

Try logging the boolean output of (mAdView.getAdSize() == null || mAdView.getAdUnitId() == null) before calling loadAd to determine its state:

    mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.BANNER);
    mAdView.setAdUnitId(AD_UNIT_ID);
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .build();
    if(mAdView.getAdSize() != null || mAdView.getAdUnitId() != null)
    mAdView.loadAd(adRequest);
   // else Log state of adsize/adunit
((LinearLayout)findViewById(R.id.adView)).addView(mAdview);
2
  • What if I have ConstraintLayout, how to add AdView to the ConstraintLayout?
    – Dyno Cris
    Commented Aug 16, 2021 at 14:14
  • I solved. Solution with ConstraintLayout put below
    – Dyno Cris
    Commented Aug 16, 2021 at 14:40
1

If you using ConstraintLayout or CoordinatorLayout solution here

For ConstraintLayout

private AdView mAdView;
private AdRequest mAdRequest;

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

    addAdView();
    initAd();
}

private void addAdView() {
    ConstraintLayout constraintLayout = findViewById(R.id.constraint_layout);
    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(constraintLayout);

    mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.BANNER);
    mAdView.setAdUnitId(getString(R.string.banner1_id));

    mAdView.setId(View.generateViewId());
    constraintLayout.addView(mAdView);
    constraintSet.connect(mAdView.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
    constraintSet.connect(mAdView.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);
    constraintSet.connect(mAdView.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0);
    constraintSet.constrainHeight(mAdView.getId(), ConstraintSet.WRAP_CONTENT);
    constraintSet.constrainWidth(mAdView.getId(), ConstraintSet.WRAP_CONTENT);
    constraintSet.applyTo(constraintLayout);

}

private void initAd() {
    MobileAds.initialize(this, initializationStatus -> {
    });

    initBanner();

}

private void initBanner() {
    mAdRequest = new AdRequest.Builder().build();
    mAdView.loadAd(mAdRequest);
    mAdView.setAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(LoadAdError loadAdError) {
            super.onAdFailedToLoad(loadAdError);

        }

        @Override
        public void onAdOpened() {
            super.onAdOpened();

        }
    });
}

For CoordinatorLayout

private AdView mAdView;
private AdRequest mAdRequest;

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

    addAdView();
    initAd();
}

private void addAdView() {
    CoordinatorLayout coordinatorLayout = findViewById(R.id.coordinator_layout);

    mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.BANNER);
    mAdView.setAdUnitId(getString(R.string.banner1_id));

    CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER | Gravity.BOTTOM;
    mAdView.setLayoutParams(params);

    mAdView.setId(View.generateViewId());

    coordinatorLayout.addView(mAdView);

}

private void initAd() {
    MobileAds.initialize(this, initializationStatus -> {
    });

    initBanner();

}

private void initBanner() {
    mAdRequest = new AdRequest.Builder().build();
    mAdView.loadAd(mAdRequest);
    mAdView.setAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(LoadAdError loadAdError) {
            super.onAdFailedToLoad(loadAdError);

        }

        @Override
        public void onAdOpened() {
            super.onAdOpened();

        }
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.