Android Implementing Admob Interstitial Ad and Banner Ad in Android App

Android Implementing Admob Interstitial Ad and Banner Ad in Android App 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.

  • 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
buildscript {
    repositories {
        google()
        mavenCentral()
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}
  • app/build.gradle:
  • dependencies {
      implementation 'com.google.android.gms:play-services-ads:21.5.0'
    }

AndroidManifest.xml

<manifest>
<application>
<!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
</application>
</manifest>
  • Initialize the Google Mobile Ads SDK  in MainActivity.java file.
private InterstitialAd mInterstitialAd;
      MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
    public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

AdRequest adRequest = new AdRequest.Builder().build();
      InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
        new InterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
        // The mInterstitialAd reference will be null until
        // an ad is loaded.
        mInterstitialAd = interstitialAd;

        Log.i(TAG, "onAdLoaded");
      }
      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        // Handle the error
        Log.d(TAG, loadAdError.toString());
        mInterstitialAd = null;
      }
    });
if (mInterstitialAd != null) {
  mInterstitialAd.show(MyActivity.this);
} else {
  Log.d("TAG", "The interstitial ad wasn't ready yet.");
}

Now add below to lines to your strings.xml 

ca-app-pub-3940256099942544/1033173712

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
  • Link

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button *