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
- Java
- Kotlin
public class ExampleActivity extends Activityimplements MaxAdViewAdListener{private MaxAdView adView;private String amazonAdSlotId;private MaxAdFormat adFormat;@Overridepublic 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 functionalint width = ViewGroup.LayoutParams.MATCH_PARENT;// Banner height on phones and tablets is 50 and 90, respectivelyint heightPx = getResources().getDimensionPixelSize( R.dimen.banner_height );adView.setLayoutParams( new FrameLayout.LayoutParams( width, heightPx ) );// Set background or background color for banners to be fully functionaladView.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 tabletsAppLovinSdkUtils.Size rawSize = adFormat.getSize();DTBAdSize size = new DTBAdSize(rawSize.getWidth(), rawSize.getHeight(), amazonAdSlotId);DTBAdRequest adLoader = new DTBAdRequest();adLoader.setSizes(size);adLoader.loadAd(new DTBAdCallback() {@Overridepublic void onSuccess(@NonNull final DTBAdResponse dtbAdResponse){// 'adView' is your instance of MaxAdViewadView.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );adView.loadAd();}@Overridepublic void onFailure(@NonNull final AdError adError) {// 'adView' is your instance of MaxAdViewadView.setLocalExtraParameter( "amazon_ad_error", adError );adView.loadAd();}});}// MAX Ad Listener@Overridepublic void onAdLoaded(final MaxAd maxAd) {}@Overridepublic void onAdLoadFailed(final String adUnitId, final MaxError error) {}@Overridepublic void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) {}@Overridepublic void onAdClicked(final MaxAd maxAd) {}@Overridepublic void onAdExpanded(final MaxAd maxAd) {}@Overridepublic void onAdCollapsed(final MaxAd maxAd) {}@Overridepublic void onAdDisplayed(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }@Overridepublic 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
- Java
- Kotlin
public class ExampleActivity extends Activityimplements MaxAdViewAdListener{private MaxAdView adView;private String amazonAdSlotId;@Overridepublic 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 tabletsint 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 functionaladView.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(){@Overridepublic void onSuccess(@NonNull final DTBAdResponse dtbAdResponse){// 'adView' is your instance of MaxAdViewadView.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );adView.loadAd();}@Overridepublic void onFailure(@NonNull final AdError adError){// 'adView' is your instance of MaxAdViewadView.setLocalExtraParameter( "amazon_ad_error", adError );adView.loadAd();}} );}// MAX Ad Listener@Overridepublic void onAdLoaded(final MaxAd maxAd) {}@Overridepublic void onAdLoadFailed(final String adUnitId, final MaxError error) {}@Overridepublic void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) {}@Overridepublic void onAdClicked(final MaxAd maxAd) {}@Overridepublic void onAdExpanded(final MaxAd maxAd) {}@Overridepublic void onAdCollapsed(final MaxAd maxAd) {}@Overridepublic void onAdDisplayed(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }@Overridepublic 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
- Java
- Kotlin
public class MyApplication extends Application{private final ExampleAppOpenManager appOpenManager;@Overridepublic void onCreate(){super.onCreate();AppLovinSdk.initializeSdk( this, new AppLovinSdk.SdkInitializationListener(){@Overridepublic void onSdkInitialized(final AppLovinSdkConfiguration configuration){appOpenManager = new ExampleAppOpenManager( this );}} );}}public class ExampleAppOpenManagerimplements 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();}@Overridepublic void onAdLoaded(final MaxAd ad) {}@Overridepublic void onAdLoadFailed(final String adUnitId, final MaxError error) {}@Overridepublic void onAdDisplayed(final MaxAd ad) {}@Overridepublic void onAdClicked(final MaxAd ad) {}@Overridepublic void onAdHidden(final MaxAd ad){appOpenAd.loadAd();}@Overridepublic void onAdDisplayFailed(final MaxAd ad, final MaxError error){appOpenAd.loadAd();}}
Interstitial Ads
Load an Interstitial Ad
- Java
- Kotlin
public class ExampleActivity extends Activityimplements 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(){@Overridepublic void onSuccess(@NonNull final DTBAdResponse dtbAdResponse){// 'interstitialAd' is your instance of MaxInterstitialAdinterstitialAd.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );interstitialAd.loadAd();}@Overridepublic void onFailure(@NonNull final AdError adError){// 'interstitialAd' is your instance of MaxInterstitialAdinterstitialAd.setLocalExtraParameter( "amazon_ad_error", adError );interstitialAd.loadAd();}} );} else {interstitialAd.loadAd();}}// MAX Ad Listener@Overridepublic void onAdLoaded(final MaxAd maxAd){// Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'}@Overridepublic void onAdLoadFailed(final String adUnitId, final MaxError error){// Interstitial ad failed to load}@Overridepublic void onAdDisplayFailed(final MaxAd maxAd, final MaxError error){// Interstitial ad failed to display.}@Overridepublic void onAdDisplayed(final MaxAd maxAd) {}@Overridepublic void onAdClicked(final MaxAd maxAd) {}@Overridepublic void onAdHidden(final MaxAd maxAd){}}
Showing an Interstitial Ad
- Java
- Kotlin
if ( interstitialAd.isReady() ){interstitialAd.showAd();}
Rewarded Ads
Load a Rewarded Ad
- Java
- Kotlin
public class ExampleActivity extends Activityimplements MaxRewardedAdListener{private MaxRewardedAd rewardedAd;private int retryAttempt;void createRewardedAd(){rewardedAd = MaxRewardedAd.getInstance( "«ad-unit-ID»", this );rewardedAd.setListener( this );rewardedAd.loadAd();}// MAX Ad Listener@Overridepublic void onAdLoaded(final MaxAd maxAd){// Rewarded ad is ready to be shown. rewardedAd.isReady() will now return 'true'// Reset retry attemptretryAttempt = 0;}@Overridepublic 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(){@Overridepublic void run(){rewardedAd.loadAd();}}, delayMillis );}@Overridepublic void onAdDisplayFailed(final MaxAd maxAd, final MaxError error){// Rewarded ad failed to display. AppLovin recommends that you load the next ad.rewardedAd.loadAd();}@Overridepublic void onAdDisplayed(final MaxAd maxAd) {}@Overridepublic void onAdClicked(final MaxAd maxAd) {}@Overridepublic void onAdHidden(final MaxAd maxAd){// rewarded ad is hidden. Pre-load the next adrewardedAd.loadAd();}@Overridepublic void onUserRewarded(final MaxAd maxAd, final MaxReward maxReward){// Rewarded ad was displayed and user should receive the reward}}
Showing a Rewarded Ad
- Java
- Kotlin
if ( rewardedAd.isReady() ){rewardedAd.showAd();}