Basic Ad Setup
suggest changeAdd the following to your dependencies:
compile 'com.google.firebase:firebase-ads:10.2.1'
and then put this in the same file.
apply plugin: 'com.google.gms.google-services'
Add the ad unit id to res/values/strings.xml.
<string name="banner_ad_unit_id">ca-app-pub-####/####</string>
Place an AdView where you want the banner and style it like any other view. The ads attributes need the ads namespace.
<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="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
Load the ad from onCreate:
MobileAds.initialize(getApplicationContext(), "ca-app-pub-YOUR_ID");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
That shows a small banner. Add more AdViews where you need more banners.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents