Banner&MREC Ads
Once the initialization of the SDK is complete, you can get and load banner and mrec ads from the Empower AppLovin SDK.
Loading a Banner or MREC
Banners
- Java
- Kotlin
public class ExampleActivity extends Activity
implements MaxAdViewAdListener
{
private MaxAdView adView;
private String amazonAdSlotId;
private MaxAdFormat adFormat;
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
adView = new MaxAdView( "«ad-unit-ID»", this );
adView.setListener( this );
// Stretch to the width of the screen for banners to be fully functional
int width = ViewGroup.LayoutParams.MATCH_PARENT;
// Banner height on phones and tablets is 50 and 90, respectively
int heightPx = getResources().getDimensionPixelSize( R.dimen.banner_height );
adView.setLayoutParams( new FrameLayout.LayoutParams( width, heightPx ) );
// Set background or background color for banners to be fully functional
adView.setBackgroundColor(…);
ViewGroup rootView = findViewById( android.R.id.content );
rootView.addView(adView);
if(AppLovinSdkUtils.isTablet(getApplicationContext()) {
amazonAdSlotId = "«Amazon-leader-slot-ID»";
adFormat = MaxAdFormat.LEADER;
} else {
amazonAdSlotId = "«Amazon-banner-slot-ID»";
adFormat = MaxAdFormat.BANNER;
}
//Raw size will be 320x50 for BANNERs on phones, and 728x90 for LEADERs on tablets
AppLovinSdkUtils.Size rawSize = adFormat.getSize();
DTBAdSize size = new DTBAdSize(rawSize.getWidth(), rawSize.getHeight(), amazonAdSlotId);
DTBAdRequest adLoader = new DTBAdRequest();
adLoader.setSizes(size);
adLoader.loadAd(new DTBAdCallback() {
@Override
public void onSuccess(@NonNull final DTBAdResponse dtbAdResponse)
{
// 'adView' is your instance of MaxAdView
adView.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );
adView.loadAd();
}
@Override
public void onFailure(@NonNull final AdError adError) {
// 'adView' is your instance of MaxAdView
adView.setLocalExtraParameter( "amazon_ad_error", adError );
adView.loadAd();
}});
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd) {}
@Override
public void onAdLoadFailed(final String adUnitId, final MaxError error) {}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdExpanded(final MaxAd maxAd) {}
@Override
public void onAdCollapsed(final MaxAd maxAd) {}
@Override
public void onAdDisplayed(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
@Override
public void onAdHidden(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
class ExampleActivity : Activity(), MaxAdViewAdListener
{
private var adView: MaxAdView? = null
private var amazonSlotId: String = ""
private latienit var adFormat: MaxAdFormat
override fun onCreate(savedInstanceState: Bundle?) {
adView = MaxAdView("«ad-unit-ID»", this)
adView.setListener(this)
// Stretch to the width of the screen for banners to be fully functional
val width = ViewGroup.LayoutParams.MATCH_PARENT
// Banner height on phones and tablets is 50 and 90, respectively
val heightPx = resources.getDimensionPixelSize(R.dimen.banner_height)
adView?.layoutParams = FrameLayout.LayoutParams(width, heightPx)
val rootView = findViewById(android.R.id.content)
rootView.addView(adView)
if(AppLovinSdkUtils.isTablet(applicationContext)) {
amazonAdSlotId = ""«Amazon-leader-slot-ID»""
adFormat = MaxAdFormat.LEADER
} else {
amazonAdSlotId = "«Amazon-banner-slot-ID»"
adFormat = MaxAdFormat.BANNER
}
// Raw size will be 320x50 for BANNERs on phones, and 728x90 for LEADERs on tablets
val rawSize = adFormat.size
val size = DTBAdSize(rawSize.width, rawSize.height, amazonAdSlotId)
val adLoader = DTBAdRequest()
adLoader.setSizes(size)
adLoader.loadAd(object : DTBAdCallback
{
override fun onSuccess(dtbAdResponse: DTBAdResponse)
{
// 'adView' is your instance of MaxAdView
adView?.setLocalExtraParameter("amazon_ad_response", dtbAdResponse)
adView?.loadAd()
}
override fun onFailure(adError: AdError)
{
// 'adView' is your instance of MaxAdView
adView?.setLocalExtraParameter("amazon_ad_error", adError)
adView?.loadAd()
}
})
}
// MAX Ad Listener
override fun onAdLoaded(maxAd: MaxAd) {}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) {}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdExpanded(maxAd: MaxAd) {}
override fun onAdCollapsed(maxAd: MaxAd) {}
override fun onAdDisplayed(maxAd: MaxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
override fun onAdHidden(maxAd: MaxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
MRECs
- Java
- Kotlin
public class ExampleActivity extends Activity
implements MaxAdViewAdListener
{
private MaxAdView adView;
private String amazonAdSlotId;
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
adView = new MaxAdView( "«ad-unit-ID»", MaxAdFormat.MREC, this );
adView.setListener( this );
// MREC width and height are 300 and 250 respectively, on phones and tablets
int widthPx = AppLovinSdkUtils.dpToPx( this, 300 );
int heightPx = AppLovinSdkUtils.dpToPx( this, 250 );
adView.setLayoutParams( new FrameLayout.LayoutParams( widthPx, heightPx ) );
// Set background or background color for MRECs to be fully functional
adView.setBackgroundColor( ... );
ViewGroup rootView = findViewById( android.R.id.content );
rootView.addView( adView );
DTBAdRequest adLoader = new DTBAdRequest();
adLoader.setSizes( new DTBAdSize( 300, 250, amazonAdSlotId ) );
adLoader.loadAd( new DTBAdCallback()
{
@Override
public void onSuccess(@NonNull final DTBAdResponse dtbAdResponse)
{
// 'adView' is your instance of MaxAdView
adView.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );
adView.loadAd();
}
@Override
public void onFailure(@NonNull final AdError adError)
{
// 'adView' is your instance of MaxAdView
adView.setLocalExtraParameter( "amazon_ad_error", adError );
adView.loadAd();
}
} );
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd) {}
@Override
public void onAdLoadFailed(final String adUnitId, final MaxError error) {}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdExpanded(final MaxAd maxAd) {}
@Override
public void onAdCollapsed(final MaxAd maxAd) {}
@Override
public void onAdDisplayed(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
@Override
public void onAdHidden(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
class ExampleActivity : Activity(), MaxAdViewAdListener
{
private var adView: MaxAdView? = null
private var amazonAdSlotId: String = ""
override fun onCreate(savedInstanceState: Bundle?) {
adView = MaxAdView("«ad-unit-ID»", MaxAdFormat.MREC, this)
adView?.setListener(this)
// MREC width and height are 300 and 250 respectively, on phones and tablets
val widthPx = AppLovinSdkUtils.dpToPx(this, 300)
val heightPx = AppLovinSdkUtils.dpToPx(this, 250)
adView?.layoutParams = FrameLayout.LayoutParams(widthPx, heightPx)
// Set background or background color for MRECs to be fully functional
adView?.setBackgroundColor(...)
// This is the root view where you want to add the ad inside it.
val rootView = findViewById(android.R.id.content)
rootView.addView(adView)
val adLoader = DTBAdRequest()
adLoader.setSizes(DTBAdSize(300, 250, amazonAdSlotId))
adLoader.loadAd(object : DTBAdCallback
{
override fun onSuccess(dtbAdResponse: DTBAdResponse)
{
// 'adView' is your instance of MaxAdView
adView!!.setLocalExtraParameter("amazon_ad_response", dtbAdResponse)
adView.loadAd()
}
override fun onFailure(adError: AdError)
{
// 'adView' is your instance of MaxAdView
adView!!.setLocalExtraParameter("amazon_ad_error", adError)
adView.loadAd()
}
})
}
// MAX Ad Listener
override fun onAdLoaded(maxAd: MaxAd) {}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) {}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdExpanded(maxAd: MaxAd) {}
override fun onAdCollapsed(maxAd: MaxAd) {}
override fun onAdDisplayed(maxAd: MaxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
override fun onAdHidden(maxAd: MaxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
App Open Ads
- Java
- Kotlin
public class MyApplication extends Application
{
private final ExampleAppOpenManager appOpenManager;
@Override
public void onCreate()
{
super.onCreate();
AppLovinSdk.initializeSdk( this, new AppLovinSdk.SdkInitializationListener()
{
@Override
public void onSdkInitialized(final AppLovinSdkConfiguration configuration)
{
appOpenManager = new ExampleAppOpenManager( this );
}
} );
}
}
public class ExampleAppOpenManager
implements LifecycleObserver, MaxAdListener
{
private final MaxAppOpenAd appOpenAd;
private final Context context;
public AppOpenManager(final Context context)
{
ProcessLifecycleOwner.get().getLifecycle().addObserver( this );
this.context = context;
appOpenAd = new MaxAppOpenAd( "«ad-unit-ID»", context);
appOpenAd.setListener( this );
appOpenAd.loadAd();
}
private void showAdIfReady()
{
if ( appOpenAd == null || !AppLovinSdk.getInstance( context ).isInitialized() ) return;
if ( appOpenAd.isReady() )
{
appOpenAd.showAd( "«test_placement»" );
}
else
{
appOpenAd.loadAd();
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart()
{
showAdIfReady();
}
@Override
public void onAdLoaded(final MaxAd ad) {}
@Override
public void onAdLoadFailed(final String adUnitId, final MaxError error) {}
@Override
public void onAdDisplayed(final MaxAd ad) {}
@Override
public void onAdClicked(final MaxAd ad) {}
@Override
public void onAdHidden(final MaxAd ad)
{
appOpenAd.loadAd();
}
@Override
public void onAdDisplayFailed(final MaxAd ad, final MaxError error)
{
appOpenAd.loadAd();
}
}
class MyApplication : Application()
{
private lateinit var appOpenManager: ExampleAppOpenManager
override fun onCreate()
{
super.onCreate()
AppLovinSdk.getInstance( this ).initializeSdk({ configuration: AppLovinSdkConfiguration ->
{
appOpenManager = ExampleAppOpenManager(applicationContext)
})
}
}
class ExampleAppOpenManager(applicationContext: Context?) : LifecycleObserver, MaxAdListener
{
private lateinit var appOpenAd: MaxAppOpenAd
private lateinit var context: Context
init
{
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
context = applicationContext
appOpenAd = MaxAppOpenAd("«ad-unit-ID»", applicationContext!!)
appOpenAd.setListener(this)
appOpenAd.loadAd()
}
private fun showAdIfReady()
{
if (appOpenAd == null || !AppLovinSdk.getInstance(context).isInitialized) return
if (appOpenAd.isReady)
{
appOpenAd.showAd("«test_placement»")
}
else
{
appOpenAd.loadAd()
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart()
{
showAdIfReady()
}
override fun onAdLoaded(ad: MaxAd) {}
override fun onAdLoadFailed(adUnitId: String, error: MaxError) {}
override fun onAdDisplayed(ad: MaxAd) {}
override fun onAdClicked(ad: MaxAd) {}
override fun onAdHidden(ad: MaxAd)
{
appOpenAd.loadAd()
}
override fun onAdDisplayFailed(ad: MaxAd, error: MaxError)
{
appOpenAd.loadAd()
}
}
Interstitial Ads
Load an Interstitial Ad
- Java
- Kotlin
public class ExampleActivity extends Activity
implements MaxAdListener
{
private static MaxInterstitialAd interstitialAd;
private static boolean isFirstLoad = true;
void createInterstitialAd()
{
interstitialAd = new MaxInterstitialAd( "«ad-unit-ID»", this );
interstitialAd.setListener( this );
if(isFirstLoad) {
isFirstLoad = false;
}
if (interstitialAd == null)
{
interstitialAd = new MaxInterstitialAd( "«MAX-inter-ad-unit-ID»", this );
}
DTBAdRequest adLoader = new DTBAdRequest();
adLoader.setSizes( new DTBAdSize.DTBInterstitialAdSize( "«Amazon-inter-slot-ID»" ) );
adLoader.loadAd( new DTBAdCallback()
{
@Override
public void onSuccess(@NonNull final DTBAdResponse dtbAdResponse)
{
// 'interstitialAd' is your instance of MaxInterstitialAd
interstitialAd.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );
interstitialAd.loadAd();
}
@Override
public void onFailure(@NonNull final AdError adError)
{
// 'interstitialAd' is your instance of MaxInterstitialAd
interstitialAd.setLocalExtraParameter( "amazon_ad_error", adError );
interstitialAd.loadAd();
}
} );
} else {
interstitialAd.loadAd();
}
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd)
{
// Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'
}
@Override
public void onAdLoadFailed(final String adUnitId, final MaxError error)
{
// Interstitial ad failed to load
}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error)
{
// Interstitial ad failed to display.
}
@Override
public void onAdDisplayed(final MaxAd maxAd) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdHidden(final MaxAd maxAd)
{
}
}
class ExampleActivity : Activity(), MaxAdListener
{
private var interstitialAd: MaxInterstitialAd? = null // static to ensure only one instance exists
private var isFirstLoad = true
fun createInterstitialAd()
{
interstitialAd = MaxInterstitialAd( "«ad-unit-ID»", this )
interstitialAd.setListener( this )
if (isFirstLoad)
{
isFirstLoad = false
if (interstitialAd == null)
{
interstitialAd = MaxInterstitialAd("«MAX-inter-ad-unit-iD»", this)
}
val adLoader = DTBAdRequest()
// Switch video player width and height values(320, 480) depending on device orientation
adLoader.setSizes(DTBAdSize.DTBVideo(320, 480, "«Amazon-video-inter-slot-ID»"))
adLoader.loadAd(object : DTBAdCallback
{
override fun onSuccess(dtbAdResponse: DTBAdResponse)
{
// 'interstitialAd' is your instance of MaxInterstitialAd
interstitialAd!!.setLocalExtraParameter("amazon_ad_response", dtbAdResponse)
interstitialAd!!.loadAd()
}
override fun onFailure(adError: AdError)
{
// 'interstitialAd' is your instance of MaxInterstitialAd
interstitialAd!!.setLocalExtraParameter("amazon_ad_error", adError)
interstitialAd!!.loadAd()
}
})
}
else
{
interstitialAd!!.loadAd()
}
}
// MAX Ad Listener
override fun onAdLoaded(maxAd: MaxAd)
{
// Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'
}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?)
{
// Interstitial ad failed to load
}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?)
{
// Interstitial ad failed to display.
}
override fun onAdDisplayed(maxAd: MaxAd) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdHidden(maxAd: MaxAd) {}
}
Showing an Interstitial Ad
- Java
- Kotlin
if ( interstitialAd.isReady() )
{
interstitialAd.showAd();
}
if ( interstitialAd.isReady )
{
interstitialAd.showAd()
}
Rewarded Ads
Load a Rewarded Ad
- Java
- Kotlin
public class ExampleActivity extends Activity
implements MaxRewardedAdListener
{
private MaxRewardedAd rewardedAd;
private int retryAttempt;
void createRewardedAd()
{
rewardedAd = MaxRewardedAd.getInstance( "«ad-unit-ID»", this );
rewardedAd.setListener( this );
rewardedAd.loadAd();
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd)
{
// Rewarded ad is ready to be shown. rewardedAd.isReady() will now return 'true'
// Reset retry attempt
retryAttempt = 0;
}
@Override
public void onAdLoadFailed(final String adUnitId, final MaxError error)
{
// Rewarded 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, Math.min( 6, retryAttempt ) ) );
new Handler().postDelayed( new Runnable()
{
@Override
public void run()
{
rewardedAd.loadAd();
}
}, delayMillis );
}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error)
{
// Rewarded ad failed to display. AppLovin recommends that you load the next ad.
rewardedAd.loadAd();
}
@Override
public void onAdDisplayed(final MaxAd maxAd) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdHidden(final MaxAd maxAd)
{
// rewarded ad is hidden. Pre-load the next ad
rewardedAd.loadAd();
}
@Override
public void onUserRewarded(final MaxAd maxAd, final MaxReward maxReward)
{
// Rewarded ad was displayed and user should receive the reward
}
}
class ExampleActivity : Activity(), MaxRewardedAdListener
{
private lateinit var rewardedAd: MaxRewardedAd
private var retryAttempt = 0.0
fun createRewardedAd()
{
rewardedAd = MaxRewardedAd.getInstance( "«ad-unit-ID»", this )
rewardedAd.setListener( this )
rewardedAd.loadAd()
}
// MAX Ad Listener
override fun onAdLoaded(maxAd: MaxAd)
{
// Rewarded ad is ready to be shown. rewardedAd.isReady() will now return 'true'
// Reset retry attempt
retryAttempt = 0.0
}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?)
{
// Rewarded 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() )
Handler().postDelayed( { rewardedAd.loadAd() }, delayMillis )
}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?)
{
// Rewarded ad failed to display. AppLovin recommends that you load the next ad.
rewardedAd.loadAd()
}
override fun onAdDisplayed(maxAd: MaxAd) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdHidden(maxAd: MaxAd)
{
// rewarded ad is hidden. Pre-load the next ad
rewardedAd.loadAd()
}
override fun onUserRewarded(maxAd: MaxAd, maxReward: MaxReward)
{
// Rewarded ad was displayed and user should receive the reward
}
}
Showing a Rewarded Ad
- Java
- Kotlin
if ( rewardedAd.isReady() )
{
rewardedAd.showAd();
}
if ( rewardedAd.isReady )
{
rewardedAd.showAd()
}