How To Insert Both Banner And Interstitial Ads In Android Applications
In my previous post, I have shown you how to insert AdMob Banner Ad and Interstitial Ad in your android app separately. In this post, I will guide you in placing both Banner And Interstitial Ads together. So, let’s get started.
I have assumed that you are using Android Studio for developing your app and your project is opened in it. Follow the below steps and copy-paste the code into your project carefully.
⟩⟩ First of all, you need to add below line of code in your build.gradle dependencies file.
compile 'com.google.android.gms:play-services-ads:8.4.0'
after adding the above code you will get sync now option at the top right corner of the page. Click on that and wait for a few minutes to complete the process, once the gradle building is finished you are ready to go with the next steps.
⟩⟩ Now go to activity_main.xml file and paste the following code in your layout.
<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>
and in the same file i.e activity_main.xml add below lines of code in the header part
xmlns:ads="http://schemas.android.com/apk/res-auto"
⟩⟩ Then open the Manifest file and add Internet permission to it.
<uses-permission android:name="android.permission.INTERNET" />
⟩⟩ Now open MainActivity.java file and add only line number 2 & 3 in public class as shown below in bold.
public class MainActivity extends AppCompatActivity { InterstitialAd mInterstitialAd; private InterstitialAd interstitial; }
⟩⟩ In the same file i.e MainActivity.java add below lines of code in onCreate() method
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(); } });
⟩⟩ Then add below lines of code before the last curly bracket of MainActivity.java
public void displayInterstitial() { // If Ads are loaded, show Interstitial else show nothing. if (interstitial.isLoaded()) { interstitial.show(); } }
⟩⟩ Now open string.xml file in values folder and paste the below line of code.
<string name="banner_ad_unit_id">ca-app-pub-3xxx256099xxxxxx/63009xxxxx</string> <string name="admob_interstitial_id">ca-app-pub-3xxx25609xxxxxx/10331xxxxx</string>
AdMob ID used here is a sample ID. In order to get revenue, you need to replace this test ID with your own AdMob Ad ID.
If you don’t know how to get your own Ad Id then check the guide on how to get AdMob Ad Unit ID.
Note: Don’t use your real Ad Unit ID while testing and never click on your own Ads it is against AdMob Policy.
That’s it, friends, this is the correct and simple way to insert both the Banner ad and Interstitial Ad in your Application altogether. Just follow the above steps and copy-paste the code carefully at the correct location mentioned above.
If you are a beginner you will love to learn Android Application development from scratch.
Hope you liked this article. If you loved this article please do comment below and share the article to support me. If you have any suggestion please feel free to comment below.
Thank you for this. But after following your instructions, I don’t get the test ads to run. I already had Interstitial ads I was trying to add banner ads. Any idea of what I could be missing?
Hello George,
Banner ca-app-pub-3940256099942544/6300978111
Interstitial ca-app-pub-3940256099942544/1033173712
Interstitial Video ca-app-pub-3940256099942544/8691691433
Rewarded Video ca-app-pub-3940256099942544/5224354917
Native Advanced ca-app-pub-3940256099942544/2247696110
Native Advanced Video ca-app-pub-3940256099942544/1044960115
Here is the test ad unit id you can use them as per the requirement.
Thank you for your reply sir. If it’s not too much to ask, can I please get the source code for the above?