In Previous posts I showed you how to implement Admob Banner Ad & Interstitial Ad to your Android application individually. Now i will show you How to implement both Admob Banner Ad and Interstitial Ad combined to your Android Application.

Also Read:

Android working with AdMob Interstitial Ad

Android working with AdMob Banner Ad

Procedure:

  • Create a new project or open your existing Android project in Android Studio 
  • Then ad below line of code in build.gradle inside your dependencies and sync project before proceeding further
compile 'com.google.android.gms:play-services-ads:8.4.0'
  • Now open your activity layout and add below lines of code. In my case I am pasting it in activity_main.xml file.
<com.google.android.gms.ads.AdView
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>

 

  • Open MainActivity.java file  and add below two lines to your main class.
InterstitialAd mInterstitialAd;
private InterstitialAd interstitial;
  • Now add below lines to your onCreate method in MainActivity.java file.
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));

interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
    public void onAdLoaded() {
        // Call displayInterstitial() function
        displayInterstitial();
    }
});
  • Add below method to your MainActivity.java file. If you don’t know exactly where to add code just paste above last curly bracket in the code or watch video embedded above.
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
  • Now add below to lines to your strings.xml 
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
<string name="admob_interstitial_id">ca-app-pub-3940256099942544/1033173712</string>

 

Note: Admob ad ids i am using here are test ad ids, you can get working ad ids from you Admob account.

  • Now run the application and test your self on AVD or Android device, you will get both Admob Banner ad & Admob Interstitial Ad

Also Read:

How to show Admob Interstitial Ad After preset time

Click On below Link to get Source Code

[ihc-hide-content ihc_mb_type=”show” ihc_mb_who=”reg” ihc_mb_template=”1″ ]Download[/ihc-hide-content]