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
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.AdViewandroid: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 Adinterstitial = new InterstitialAd(MainActivity.this);// Insert the Ad Unit IDinterstitial.setAdUnitId(getString(R.string.admob_interstitial_id));interstitial.loadAd(adRequest);// Prepare an Interstitial Ad Listenerinterstitial.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
- Link