The following sections show you how to load and then show and hide a banner or MREC ad.
Loading a Banner or MREC
To load a banner or MREC, create a MaxAdView
object that corresponds to your ad unit.
Then call call that object’s loadAd()
method.
To show that ad, add the MaxAdView
object as a subview of your view hierarchy.
Implement MaxAdViewAdListener
so that you are notified when your ad is ready (you will also be notified of other ad-related events).
If your integration requires displaying MREC ads in a content feed, AppLovin recommends the following:
Create the minimal amount of instances possible.
Stop auto-refresh.
Manually refresh the contents by calling loadAd()
(re-use the MaxAdView
instances).
You can find an example implementation in the AppLovin demo app (Java , Kotlin ).
Banners
public class ExampleActivity extends Activity
implements MaxAdViewAdListener
private MaxAdView adView ;
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 color for banners to be fully functional
adView . setBackgroundColor ( «background - color» ) ;
ViewGroup rootView = findViewById ( android . R . id . content ) ;
rootView . addView ( adView ) ;
public void onAdLoaded ( final MaxAd maxAd ) {}
public void onAdLoadFailed ( final String adUnitId , final MaxError error ) {}
public void onAdDisplayFailed ( final MaxAd maxAd , final MaxError error ) {}
public void onAdClicked ( final MaxAd maxAd ) {}
public void onAdExpanded ( final MaxAd maxAd ) {}
public void onAdCollapsed ( final MaxAd maxAd ) {}
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 */ }
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
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)
// Set background color for banners to be fully functional
adView?. setBackgroundColor ( «background - color» )
val rootView = findViewById (android.R.id.content)
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
public class ExampleActivity extends Activity
implements MaxAdViewAdListener
private MaxAdView adView ;
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 color for MRECs to be fully functional
adView . setBackgroundColor ( «background - color» ) ;
ViewGroup rootView = findViewById ( android . R . id . content ) ;
rootView . addView ( adView ) ;
public void onAdLoaded ( final MaxAd maxAd ) {}
public void onAdLoadFailed ( final String adUnitId , final MaxError error ) {}
public void onAdDisplayFailed ( final MaxAd maxAd , final MaxError error ) {}
public void onAdClicked ( final MaxAd maxAd ) {}
public void onAdExpanded ( final MaxAd maxAd ) {}
public void onAdCollapsed ( final MaxAd maxAd ) {}
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 */ }
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
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 color for MRECs to be fully functional
adView?. setBackgroundColor ( «background - color» )
val rootView = findViewById (android.R.id.content)
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 */ }
Loading and Showing a Banner or MREC in Layout Editor
Alternatively, you can add MAX banners or MRECs to your view layout XML.
Ensure that your ads are fully functional by setting a background or background color (android:background
).
In the case of banners, stretch the width (android:layout_width
) to the width of the screen.
In the case of MRECs, set android:adFormat
accordingly:
< com.applovin.mediation.ads.MaxAdView
xmlns:maxads = " http://schemas.applovin.com/android/1.0 "
maxads:adUnitId = " «ad-unit-ID» "
android:background = " @color/banner_background_color "
android:layout_width = " match_parent "
android:layout_height = " @dimen/banner_height " />
Declare the base banner height of 50dp in res/values/attrs.xml
:
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< dimen name = " banner_height " > 50dp </ dimen >
Declare the tablet banner height of 90dp in res/values-sw600dp/attrs.xml
:
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< dimen name = " banner_height " > 90dp </ dimen >
< com.applovin.mediation.ads.MaxAdView
xmlns:maxads = " http://schemas.applovin.com/android/1.0 "
maxads:adUnitId = " «ad-unit-ID» "
android:background = " @color/mrec_background_color "
android:layout_width = " 300dp "
android:layout_height = " 250dp " />
You still must call loadAd()
on the MaxAdView
you create in this way:
MaxAdView adView = findViewById ( R . id . ad_view ) ;
var adView: MaxAdView = findViewById (R.id.ad_view)
Destroying Banners and MRECs
If you no longer need a MaxAdView
instance, call its destroy()
method to free resources.
This may be the case, for example, if the user purchases ad removal.
Do not call the destroy()
method if you use multiple instances with the same Ad Unit ID.
Adaptive Banners
Note
Only Google bidding and Google AdMob, and Google Ad Manager support adaptive banners.
MAX sizes banners from other networks normally.
Google recommends that developers include a 50px padding between the banner placement and the app content.
This makes it less likely that users will accidentally click on the banner.
Refer to Google’s “About Confirmed Click” policy for more information and best practices.
Adaptive banners are responsive banners with heights that derive from the device type and the width of the banner.
You integrate adaptive banners in a similar way to how you integrate regular banners.
However, you must set the height to the value returned by MAAdFormat.banner.adaptiveSize.height
rather than to a constant like 50 or 90.
Before you load the ad, set the banner extra parameter adaptive_banner
to true
as shown in the code below:
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 ;
// Get the adaptive banner height.
int heightDp = MaxAdFormat . BANNER . getAdaptiveSize ( this ) . getHeight () ;
int heightPx = AppLovinSdkUtils . dpToPx ( this , heightDp ) ;
adView . setLayoutParams ( new FrameLayout. LayoutParams ( width, heightPx ) ) ;
adView . setExtraParameter ( " adaptive_banner " , " true " ) ;
// Set background color for banners to be fully functional
adView . setBackgroundColor ( «background - color» ) ;
ViewGroup rootView = findViewById ( android . R . id . content ) ;
rootView . addView ( adView ) ;
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
// Get the adaptive banner height.
val heightDp = MaxAdFormat.BANNER. getAdaptiveSize ( this ).height
val heightPx = AppLovinSdkUtils. dpToPx ( this , heightDp)
adView?.layoutParams = FrameLayout. LayoutParams (width, heightPx)
adView?. setExtraParameter ( "adaptive_banner" , "true" )
// Set background color for banners to be fully functional
adView?. setBackgroundColor ( «background - color» )
val rootView = findViewById <ViewGroup>(android.R.id.content)
Setting a Custom Width
For more specific integrations, you can set a custom width by using the local extra parameters API (as of Google adapter version 21.5.0.3 and Google Ad Manager adapter version 21.5.0.2).
You can fetch the appropriate height for your custom adaptive banner by using the adaptive size API.
int heightDp = MaxAdFormat . BANNER . getAdaptiveSize ( width, context ) . getHeight () ;
adView . setLocalExtraParameter ( " adaptive_banner_width " , width ) ;
val heightDp = adView.adFormat. getAdaptiveSize (width, context).height
adView. setLocalExtraParameter ( "adaptive_banner_width" , width)
Inline Adaptive Banners
Adaptive banners are anchored by default.
Alternatively, you can enable inline adaptive banners, which are meant to appear in scrolling content.
Inline adaptive banners are typically larger and taller than anchored adaptive banners.
They have variable heights that can extend to the full height of the device screen.
Inline adaptive banners are supported starting with Android adapter version 23.2.0.1. To enable, set the extra local parameter adaptive_banner_type
to inline
, as shown in the code below:
adView . setLocalExtraParameter ( " adaptive_banner_type " , " inline " ) ;
adView. setLocalExtraParameter ( "adaptive_banner_type" , "inline" )
You may want to set a maximum height, in dp, for your inline adaptive banners.
You can do this with code like the following, which uses a height of 100 dp as an example:
adView . setLocalExtraParameter ( " inline_adaptive_banner_max_height " , 100 ) ;
adView. setLocalExtraParameter ( "inline_adaptive_banner_max_height" , 100 )
You may need to configure your UI in a way that depends on the adaptive banner size.
If so, you can retrieve the width and height of the loaded ad, in dp, with code like the following:
public void onAdLoaded ( final MaxAd maxAd )
AppLovinSdkUtils . Size adViewSize = maxAd . getSize () ;
int widthDp = adViewSize . getWidth () ;
int heightDp = adViewSize . getHeight () ;
override fun onAdLoaded (ad: MaxAd?)
val adViewSize = ad?.size !!
val widthDp = adViewSize.width
val heightDp = adViewSize.height
Inline Adaptive Banners in MRECs
You can use inline adaptive banners in MREC ad spaces, starting with Android adapter version 23.6.0.3.
However, for MRECs, the adaptive banner must be inline, not anchored, to function properly.
Inline adaptive banners in MREC placements span the full width of the screen and may have a variable height that can exceed the standard MREC dimensions.
You can set an optional maximum height by setting the extra local parameter inline_adaptive_banner_max_height
.
You can configure an inline adaptive banner for an MREC placement with code like the following:
adView = new MaxAdView ( " «ad-unit-ID» " , MaxAdFormat . MREC , this ) ;
adView . setListener ( this ) ;
// Stretch to the width of the screen for adaptive MRECs to be fully functional
int width = ViewGroup . LayoutParams . MATCH_PARENT ;
// The height can exceed the standard MREC height
int heightPx = AppLovinSdkUtils . dpToPx ( this , 250 ) ;
adView . setLayoutParams ( new FrameLayout. LayoutParams ( width, heightPx ) ) ;
adView . setExtraParameter ( " adaptive_banner " , " true " ) ;
adView . setLocalExtraParameter ( " adaptive_banner_type " , " inline " ) ;
// Optional: Set a maximum height for the adaptive banner.
// Google recommends a height greater than 50, with a minimum of 32 and a maximum equal to the screen height.
// The value must also not exceed the height of the MaxAdView.
adView . setLocalExtraParameter ( " inline_adaptive_banner_max_height " , 200 ) ;
// Set background color for adaptive MRECs to be fully functional
adView . setBackgroundColor ( «background - color» ) ;
ViewGroup rootView = findViewById ( android . R . id . content ) ;
rootView . addView ( adView ) ;
adView = MaxAdView ( " «ad-unit-ID» " , MaxAdFormat.MREC, this )
adView?. setListener ( this )
// Stretch to the width of the screen for adaptive MRECs to be fully functional
val width = ViewGroup.LayoutParams.MATCH_PARENT
// The height can exceed the standard MREC height
val heightPx = AppLovinSdkUtils. dpToPx ( this , 250 )
adView?.layoutParams = FrameLayout. LayoutParams (width, heightPx)
adView?. setExtraParameter ( "adaptive_banner" , "true" )
adView?. setLocalExtraParameter ( "adaptive_banner_type" , "inline" )
// Optional: Set a maximum height for the adaptive banner.
// Google recommends a height greater than 50, with a minimum of 32 and a maximum equal to the screen height.
// The value must also not exceed the height of the MaxAdView.
adView?. setLocalExtraParameter ( "inline_adaptive_banner_max_height" , 200 )
// Set background color for adaptive MRECs to be fully functional
adView?. setBackgroundColor ( «background - color» )
val rootView = findViewById (android.R.id.content)
Stopping and Starting Auto-Refresh
You may want to stop auto-refresh for an ad.
This may be the case when you hide an ad or when you want to manually refresh.
Stop auto-refresh with the following code:
// Set this extra parameter to work around SDK bug that ignores calls to stopAutoRefresh()
adView . setExtraParameter ( " allow_pause_auto_refresh_immediately " , " true " ) ;
adView . stopAutoRefresh () ;
// Set this extra parameter to work around SDK bug that ignores calls to stopAutoRefresh()
adView. setExtraParameter ( "allow_pause_auto_refresh_immediately" , "true" )
Start auto-refresh for an ad with the following code:
adView . startAutoRefresh () ;
adView. startAutoRefresh ()
Manually refresh the ad contents by calling loadAd()
.
You can do this only if you have previously stopped auto-refresh.