简体   繁体   中英

Admob banner setAdUnitId

I need to set the admob banner ad unit id programatically in java class

  mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
  mAdView.setAdSize(AdSize.SMART_BANNER);

and at the same time use xml for layout

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

I tried this but it didn't work as it shows an error: Required XML attribute “adSize” was missing

Please help

Try this

Put it on onCreate :

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

Put in Xml

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

Put in String file :

<string name="BANNER_ID">Your Banner Id</string>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM