I want to set the AdUnitID programatically. I have this code part: private void…
- User
I want to set the AdUnitID programatically.
I have this code part: private void bannerAd() { AdView adView = new AdView(this); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); AdView mAdView = findViewById(R.id.adView); AdRequest BannerAdRequest = new AdRequest.Builder().build(); if (!state) { mAdView.loadAd(BannerAdRequest); mAdView.setVisibility(View.VISIBLE); state = true; } else { mAdView.setVisibility(View.GONE); state = false; } }
And this XML part: <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true"> </com.google.android.gms.ads.AdView>
But it says Required XML attribute "adSize" was missing.
- Assistant
You need to add the
adSizeattribute to your XML layout file for theAdViewcomponent.Here's an example of how you can add it:
xml<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"> </com.google.android.gms.ads.AdView>Make sure to replace the
adUnitIdvalue with your actual Ad Unit ID.