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;@Overridepublic void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {adView = MaxAdView("«ad-unit-ID»", this)adView?.setListener(this)// Stretch to the width of the screen for banners to be fully functionalval width = ViewGroup.LayoutParams.MATCH_PARENT// Banner height on phones and tablets is 50 and 90, respectivelyval heightPx = resources.getDimensionPixelSize(R.dimen.banner_height)adView?.layoutParams = FrameLayout.LayoutParams(width, heightPx)// Set background or background color for banners to be fully functionaladView?.setBackgroundColor(…)val rootView = findViewById(android.R.id.content)rootView.addView(adView)// Load the adadView?.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;@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 );// Load the adadView.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 MaxInterstitialAd interstitialAd;private int retryAttempt;void createInterstitialAd(){interstitialAd = new MaxInterstitialAd( "«ad-unit-ID»", this );interstitialAd.setListener( this );// Load the first adinterstitialAd.loadAd();}// MAX Ad Listener@Overridepublic void onAdLoaded(final MaxAd maxAd){// Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'// Reset retry attemptretryAttempt = 0;}@Overridepublic void onAdLoadFailed(final String adUnitId, final 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, Math.min( 6, retryAttempt ) ) );new Handler().postDelayed( new Runnable(){@Overridepublic void run(){interstitialAd.loadAd();}}, delayMillis );}@Overridepublic void onAdDisplayFailed(final MaxAd maxAd, final MaxError error){// Interstitial ad failed to display. AppLovin recommends that you load the next ad.interstitialAd.loadAd();}@Overridepublic void onAdDisplayed(final MaxAd maxAd) {}@Overridepublic void onAdClicked(final MaxAd maxAd) {}@Overridepublic void onAdHidden(final MaxAd maxAd){// Interstitial ad is hidden. Pre-load the next adinterstitialAd.loadAd();}}
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();}