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

谁说胖子不能爱 提交于 2019-11-28 22:31:22
diogojme

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.

xmlns:ads="http://schemas.android.com/apk/res-auto" 

This solved my problem.

do not use this

xmlns:ads="http://schemas.android.com/tools" 

You've got

android:layout_width="wrap_content"
android:layout_height="wrap_content"

From: https://developers.google.com/admob/android/banner#smart_banners

Note: The smart banner view in your layout must consume the full width of the device. If it doesn't, you'll get a warning with the message "Not enough space to show ad", and the banner will not be displayed.

change it to

android:layout_width="match_parent"

And set your adUnitID in the xml file.

ads:adUnitId="AD_UNIT_ID"

I have made it like this

<LinearLayout xmlns:ads="http://schemas.android.com/apk/res-auto"
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">



<LinearLayout
    android:id="@+id/ll_main_screen_container"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="wrap_content"
    android:orientation="horizontal"/>

</LinearLayout>

in code

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newone);
    initAdsAccount();
    initAds();
}

private void initAdsAccount()
{
    String accountId = AdsConstants.my_fake_ads_account_id;

    if (AdsConstants.isAdsMode)
    {
        accountId = AdsConstants.my_ads_account_id;
    }

    MobileAds.initialize(this, accountId);
}

private void initAds()
{
    findViewById(R.id.ll_main_screen_container).post(//
            new Runnable()
            {
                @Override
                public void run()
                {
                    LinearLayout adContainer = findViewById(R.id.ll_main_screen_container);
                    AdView mAdView = new AdView(MainActivity.this);
                    mAdView.setAdSize(AdSize.BANNER);

                    if (AdsConstants.isAdsMode)
                    {
                        mAdView.setAdUnitId(AdsConstants.main_screen_bottom_banner_id);
                    }
                    else
                    {
                        mAdView.setAdUnitId(AdsConstants.fake_banner_id);
                    }

                    adContainer.addView(mAdView);
                    AdRequest adRequest = new AdRequest.Builder().build();
                    mAdView.loadAd(adRequest);
                }
            }//
    );
}

add this to your layout

xmlns:ads="http://schemas.android.com/apk/res-auto" 

For me, I was setting the ad type like this:

ads:adSize="Banner"

While it shall be all caps:

ads:adSize="BANNER"

After trying,

  1. Supply adunit ID and adsize from xml ONLY.
  2. Supply adunit ID and adsize from .java file ONLY.

Problem is solved by

  1. Removing xmlns:ads="http://schemas.android.com/apk/res-auto" line from root layout.
  2. supply adunit ID and adsize as follows,

    app:adSize="SMART_BANNER" app:adUnitId="@string/banner_ad_unit_id"

    1. Clean and run app.

Try to set your ad unit id in XML

ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"

Like

ads:adUnitId="ca-app-pub-3940256099942544/6300978"

or find the link for more detail https://developers.google.com/admob/android/quick-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!