How to Implement Admob Interstitial Ad in Android Application
In previous post i explained about how to implement Admob Banner Ad, in this post i will show you how to add Admob Interstitial Ad to your android app.
Note: Please read Admob policy before implementing Admob Interstitial ad. Violating Admob terms may lead to suspension of your Admob account.
Procedure
Open your Project in Android Studio
Add below line to your build.gradle in dependencies
compile 'com.google.android.gms:play-services-ads:8.4.0'
Now open MainActivity,java and add below 2 & 3 lines in public class as shown below
public class MainActivity extends AppCompatActivity {InterstitialAd mInterstitialAd;private InterstitialAd interstitial;
}
Now open MainActivity,java and add below lines inside onCreate method
AdRequest adRequest = new AdRequest.Builder().build(); // 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(); } });
Now add below method to your MainActivity.java
public void displayInterstitial() {// If Ads are loaded, show Interstitial else show nothing.if (interstitial.isLoaded()) {interstitial.show();}}
Now open strings.xml from values folder and add below string to it.
<string name="admob_interstitial_id">ca-app-pub3940256099942544/1033173712</string>
Note: Admob id used in this post for demonstration is a test ad id. Please get your interstitial admob ad id from Admob
Commentaires
Enregistrer un commentaire