Android SDK Usage

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

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 */ }
}

MRECs

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 */ }
}

App Open Ads

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();
}
}

Interstitial Ads

Load an Interstitial Ad

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)
{
}
}

Showing an Interstitial Ad

if ( interstitialAd.isReady() )
{
interstitialAd.showAd();
}

Rewarded Ads

Load a Rewarded Ad

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
}
}

Showing a Rewarded Ad

if ( rewardedAd.isReady() )
{
rewardedAd.showAd();
}