
This tutorial is a part of the AppLovin MAX integration guide. To show an interstitial ad, we need to create an ad unit. Login to your AppLovin dashboard, and, go to MAX > Ad Units section on the left panel. Next, click on the “Create Ad Unit” button.

On the next page, type a name for the ad unit. Select the Android platform and type your app name. Your app will come up in the list. Next, select Interstitial ad type. We will add the mediation networks later. Next, scroll down and click on the green color Save button.

AppLovin creates a unique id for your ad unit. Copy the id. Next, let’s do coding.
Here is the code for AppLovin Interstitial ads:
MainActivity.kt (kotlin):
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdListener
import com.applovin.mediation.MaxError
import com.applovin.mediation.ads.MaxInterstitialAd
import java.util.concurrent.TimeUnit
import com.applovin.sdk.AppLovinSdk
import com.applovin.sdk.AppLovinSdkConfiguration
class MainActivity : AppCompatActivity(), MaxAdListener {
private var retryAttempt = 0.0
private var interstitialAd: MaxInterstitialAd? = null
private var handlerRetryAd: Handler? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initializeAdNetwork() // initialize ads only once during the app startup
createInterstitialAd()
handlerRetryAd = Handler(Looper.getMainLooper())
// Load your first ad
loadInterstitialAd()
}
private fun initializeAdNetwork() {
AppLovinSdk.getInstance(getApplicationContext()).mediationProvider = "max"
AppLovinSdk.getInstance(getApplicationContext()).initializeSdk { configuration: AppLovinSdkConfiguration ->
}
}
private fun createInterstitialAd() {
interstitialAd = MaxInterstitialAd("YOUR_AD_UNIT_ID", this)
interstitialAd?.setListener(this)
}
// Call this method whenever you want to load a new ad
private fun loadInterstitialAd() {
interstitialAd?.loadAd()
}
// Call this method to show the ad
private fun showInterstitialAd() {
if (interstitialAd?.isReady == true) {
interstitialAd?.showAd()
}
}
override fun onDestroy() {
handlerRetryAd?.removeCallbacksAndMessages(null)
interstitialAd?.destroy()
super.onDestroy()
}
// The following methods are MAX Ad Listener
override fun onAdLoaded(maxAd: MaxAd) {
// Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'
// Reset retry attempt
retryAttempt = 0.0
}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) {
// Interstitial ad failed to load
// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++
val delayMillis =
TimeUnit.SECONDS.toMillis(Math.pow(2.0, Math.min(6.0, retryAttempt)).toLong())
val runnableAd = Runnable {
loadInterstitialAd()
}
handlerRetryAd?.postDelayed(runnableAd, delayMillis)
}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) {
// Interstitial ad failed to display. AppLovin recommends that you load the next ad.
loadInterstitialAd()
}
override fun onAdDisplayed(maxAd: MaxAd) {
// It is called when the ad is shown to the user
}
override fun onAdClicked(maxAd: MaxAd) {
// User clicked on the ad
}
override fun onAdHidden(maxAd: MaxAd) {
// User closed the ad. Pre-load the next ad
loadInterstitialAd()
}
}
MainActivity.java (Java):
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdListener;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.ads.MaxInterstitialAd;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity implements MaxAdListener {
private MaxInterstitialAd interstitialAd;
private Handler handlerRetryAd;
private int retryAttempt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeAdNetwork(); // initialize ads only once during the app startup
createInterstitialAd();
handlerRetryAd = new Handler();
// Load your first ad
loadInterstitialAd();
}
private void initializeAdNetwork() {
AppLovinSdk.getInstance(getApplicationContext()).setMediationProvider("max");
AppLovinSdk.initializeSdk(getApplicationContext(), new AppLovinSdk.SdkInitializationListener() {
@Override
public void onSdkInitialized(final AppLovinSdkConfiguration configuration) {
}
});
}
void createInterstitialAd() {
interstitialAd = new MaxInterstitialAd("YOUR_AD_UNIT_ID", this);
interstitialAd.setListener(this);
}
// Call this method whenever you want to load a new ad
private void loadInterstitialAd() {
interstitialAd.loadAd();
}
// Call this method to show the ad
private void showInterstitialAd() {
if (interstitialAd.isReady()) {
interstitialAd.showAd();
}
}
@Override
protected void onDestroy() {
handlerRetryAd.removeCallbacksAndMessages(null);
interstitialAd.destroy();
super.onDestroy();
}
@Override
public void onAdLoaded(MaxAd ad) {
// Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'
// Reset retry attempt
retryAttempt = 0;
}
@Override
public void onAdDisplayed(MaxAd ad) {
// It is called when the ad is shown to the user
}
@Override
public void onAdHidden(MaxAd ad) {
// User closed the ad. Pre-load the next ad
loadInterstitialAd();
}
@Override
public void onAdClicked(MaxAd ad) {
// User clicked on the ad
}
@Override
public void onAdLoadFailed(String adUnitId, MaxError error) {
// Interstitial ad failed to load
// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++;
long delayMillis =
TimeUnit.SECONDS.toMillis((long) Math.pow(2.0, Math.min(6.0, retryAttempt)));
Runnable runnableAd = new Runnable() {
@Override
public void run() {
loadInterstitialAd();
}
};
handlerRetryAd.postDelayed(runnableAd, delayMillis);
}
@Override
public void onAdDisplayFailed(MaxAd ad, MaxError error) {
// Interstitial ad failed to display. AppLovin recommends that you load the next ad.
loadInterstitialAd();
}
}
Don’t forget to replace YOUR_AD_UNIT_ID with the actual id.
If the ad doesn’t show up, check the following things:
- AppLovin SDK key in the manifest file.
- See the network you selected in the Test Device section. You should add the network’s dependency in the build.gradle file.
- Check if the YOUR_AD_UNIT_ID is correct and belongs to the app you are testing.
If you still have problem, look at the onAdLoadFailed() and onAdDisplayFailed() methods. You have access to the error object. Log the error.getMessage() to see the problem.