Skip to content

Commit

Permalink
Create interstitial_ad.md
Browse files Browse the repository at this point in the history
  • Loading branch information
rutavi authored Mar 14, 2024
1 parent eb04952 commit ebaa098
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions java_documentation/interstitial_ad.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: Interstitial ad
layout: home
parent: Java
nav_order: 4
---


## Interstitial ad
To show an interstitial ad, there is no need to add an element to the layout file of the class or fragment where the ad will be shown.
```java
private void createInterstitialAd() {
interstitialAdUnit = new InterstitialAdUnit(CONFIG_ID, WIDTH, HEIGHT);

final AdManagerAdRequest.Builder builder = new AdManagerAdRequest.Builder();
interstitialAdUnit.fetchDemand(builder, resultCode -> {
AdManagerAdRequest request = builder.build();
AdManagerInterstitialAd.load(this, AD_UNIT_ID, request, interstitialListener());
});
}
```
The context that is passed to `createInterstitialAd` method is the class where the banner will be displayed. `interstitialAdUnit` is an `InterstitialAdUnit` object; to initialize it use `CONFIG_ID`.
It is optional to specify the minimum height and width in percent of the ad, e.g. 80x60 means that the minimum width of the interstitial ad will be at least 80% of the screen and at least 60% of the height.

## Ad listener

Ad listener is used to check whether the ad was successfully loaded.
```java
private AdManagerInterstitialAdLoadCallback interstitialListener() {
return new AdManagerInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull AdManagerInterstitialAd interstitialManager) {
Log.d(Tag, "Interstitial ad loaded successfully");
interstitialManager.show(MainActivity.this);
}

@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
Log.e(Tag, "Failed to load interstitial ad: "+ loadAdError.getMessage());
}
};
}
```

0 comments on commit ebaa098

Please sign in to comment.