How To Load Interstitial Ad When Navigation Item Is Clicked
In the previous post, we have already discussed how to place Interstitial Ads in Android app. In this post, I will show you how you can place Interstitial Ad in your Navigation item so, that whenever your visitors will click on the item a full-screen Ad will open.
You can implement this code in any navigational item. So, let’s get started.
⟩⟩ 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.
⟩⟩ 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 3 in public class as shown below in bold.
public class MainActivity extends AppCompatActivity { //Add variable private InterstitialAd interstitial; }
⟩⟩ In the same file i.e MainActivity.java add below lines of code in onCreate() method
// Prepare the Interstitial Ad interstitial = new InterstitialAd(MainActivity.this);
⟩⟩ Once you added all the above code you just need to add the below codes wherever you want to load Interstitial Ad.
interstitial = new InterstitialAd(getApplicationContext()); interstitial.setAdUnitId(getString(R.string.admob_interstetial_ad)); AdRequest adRequest = new AdRequest.Builder().build(); interstitial.loadAd(adRequest); interstitial.setAdListener(new AdListener() { public void onAdLoaded() { if (interstitial.isLoaded()) { interstitial.show(); } } });
⟩⟩ Now open string.xml file and paste your Interstitial code as shown below.
ca-app-pub-3xxxx56099xxxxxx/10331xxxxx
AdMob ID used here is a sample ID. In order to get revenue, you need to replace this test ID with your own Interstitial Ad ID. If you don’t know how to get your own Interstitial Ad ID check this 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 display Interstitial Ad When Navigation Item Is Clicked. 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.
If you found this article helpful please do comment below and share the article to support me. If you have any suggestion please feel free to comment below.
Hey,
Thanks for the informatio and the code it could have been better if u coild provide a screenshot on how the add is displayed or works
It’s a great suggestion I will try to add the screenshot. 🙂 Kudos!!
Thank you for the code. It works as expected!