generated from just-the-docs/just-the-docs-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
}; | ||
} | ||
``` |