AdMob is a multi-platform mobile ad network that integrating to your app and allows you to monetize your app and earn on the basis of Impressions and clicks on ads in your app.
There are two types of AdMob ads: Banner and Interstitial. Banner ad is the one which occupies the portion of your screen which you can define in your code. Interstitial ad is the one which occupies devices full screen. If you want to go to device UI you have a cancel button to disappeare Interstitial ad.
1. Get Admob banner ad unit id from Google AdMob
2. Create new Android Application
3. To integrate Google AdMob, use Google AdMob SDK in build.gradle
compile ‘com.google.android.gms:play-services-ads:8.4.0’
4. Open Android.Manifest file and give permission for INTERNET and ACCESS_NETWORK_STATE
<
uses-permission
android:name
=
"android.permission.INTERNET"
/> <
us
es-permission
android:name
=
"android.permission.ACCESS_NETWORK_STATE"
/>
Add google play services version details in META-DATA
<
meta-data
android:name
=
"com.google.android.gms.version"
android:value
=
"@integer/google_play_services_version"
/>
Add the AdActivity adding configChanges and theme attributes.
<
activity
android:name
=
"com.google.android.gms.ads.AdActivity"
android:configChanges
=
"keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme
=
"@android:style/Theme.Translucent"
/>
5. Add code for Banner to Your layout file
< com.google.android.gms.ads.AdView android:id = "@+id/adView" android:layout_width = "wrap_content" android:layout_height = "wrap_content" ads:adSize = "BANNER" ads:adUnitId = "@string/banner ad unit id" > < com.google.android.gms.ads.AdView />
6. Open Java file and Create AdRequest to display ads. private AdView mAdView; mAdView = (AdView) findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder() .build(); mAdView.loadAd(adRequest);
7. Add code for Interstitial Ads // create private variable private InterstitialAd mInterstitialAd;// initialize variable mInterstitialAd = newInterstitialAd(); loadInterstitial();
// on button click call function to show ad
showInterstitial();
// initialize and load ad functions
private InterstitialAd newInterstitialAd() {
InterstitialAd interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
interstitialAd.setAdListener(new AdListener() {
@Override public void onAdLoaded() {
btnshow.setEnabled(true);
}
@Override public void onAdFailedToLoad(int errorCode) {
btnshow.setEnabled(true);
}
@Override public void onAdClosed() {
}
});
return interstitialAd;
}
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and reload the ad. if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
}
}
private void loadInterstitial() {
// Disable the next level button and load the ad. btnshow.setEnabled(false);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
mInterstitialAd.loadAd(adRequest);
}
8. Rebuild your project and Run your application
|