The ad size and ad unit ID must be set before loadAd when set programmatically

Mike Flynn picture Mike Flynn · Dec 11, 2015 · Viewed 23.8k times · Source

I have no idea what is going on here but I am trying to set my ad unit ID dynamically through code like below and removing it from the XML but still get the error:

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

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

and

    AdView mAdView = (AdView) rootView.findViewById(R.id.adView);
    mAdView.setAdUnitId(getEvent().getAdMobUnitId());
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

Answer

diogojme picture diogojme · Dec 11, 2015

Create it programatically

View adContainer = findViewById(R.id.adMobView);

AdView mAdView = new AdView(context);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(YOUR_BANNER_ID);
((RelativeLayout)adContainer).addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

And in your xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <RelativeLayout 
            android:id="@+id/adMobView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"/>

</RelativeLayout>

EDIT

The best practice for banners, is two show one banner per view (one unitId), if you want to show more banners in the same screens (NOT RECOMMENDED), you have to create another unitId from console and one adView for each unitId.

My answer is:

Don’t know if its a bug or if you can have only one unitId per adView and it make more sense, because you can only have one unitId per adView, and reading from docs they show two ways to do it, by instantianting a new AdView() and programtically setting the unitIds and sizes OR do it only from XML.

And I did some tests to arrive at this conclusion.

By using findViewById from your com.google.android.gms.ads.AdView

1 - You can setAdUnitId programatically if you set adSize first.

2 - You cannot setAdUnitIdprogramatically if it’s already in your xml.

3 - If you doesn’t use ’setAdUnitId’ in your xml, it will alert Required xml attribute adUnitId was missing, and the same for adSize even if you set both attributes programatically.

4 - If not put setAdUnitId and setSize and put it programtically, the adView will alert you Required xml attribute adUnitId was missing, and the same if you not set adSize in xml.

5 - The only thing programatically you can do is call mAdView.loadAd(adRequest) to load the ad

By using new AdView()

1 - It will work if you create an empty layout, then add the adView reference to this view.

2 - You can set the adSize and adUnitId programatically it will work.

3- If you try to use setAdUnitAd twice this exception will launched The ad unit ID can only be set once on AdView. the same if you use by findViewById

My conclusions are:

You can use only from XML"

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-my_id_number_was_ommited_by_security" />

and load view on onCreate

AdView mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

or full programatically

View adContainer = findViewById(R.id.adMobView);
AdView mAdView = new AdView(context);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(YOUR_BANNER_ID);
adContainer.addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

I use banners for a long time and that answer is good for me.