Loading and Showing MRECs Programmatically
To load an MREC ad, create a MaxAdView
object corresponding to your ad unit and call its loadAd()
method. To show the ad, add the MaxAdView
object as a subview of your view hierarchy. Implement MaxAdViewAdListener
so that you are notified when your ad is ready and of other ad-related events. If your integration requires displaying MREC ads in a content feed, AppLovin recommends that you create the minimal amount of instances possible, stop the auto-refresh, then manually refresh the contents by calling loadAd()
and re-use the MaxAdView
instances. You can find an example implementation in the AppLovin demo app (Java, Kotlin).
-
public class ExampleActivity extends Activity implements MaxAdViewAdListener { private MaxAdView adView; void createMrecAd { 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 ); // Load the ad 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 fun createMrecAd() { 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(...) val rootView = findViewById(android.R.id.content) rootView.addView(adView) // Load the ad 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 */ } }
Loading and Showing MRECs in Layout Editor
Alternatively, you can add MAX MRECs to your view layout XML. Ensure that your MRECs are fully functional by setting the ad format (android:adFormat
) to "MREC"
and by setting a background or background color (android:background
):
<com.applovin.mediation.ads.MaxAdView xmlns:maxads="http://schemas.applovin.com/android/1.0" maxads:adUnitId="ad_unit_ID" maxads:adFormat="MREC" android:background="@color/mrec_background_color" android:layout_width="300dp" android:layout_height="250dp" />
Destroying MRECs
After you are no longer using the MaxAdView
instance (for example, if the user purchased ad removal), call its destroy()
method to free resources. Do not call the destroy()
method if you use multiple instances with the same Ad Unit ID.
-
adView.destroy();
-
adView.destroy()
Stopping and Starting Auto-Refresh
There may be cases when you would like to stop auto-refresh, for instance, when you hide an MREC ad or want to manually refresh. Stop auto-refresh for an MREC ad 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" ) adView.stopAutoRefresh()
Start auto-refresh for an MREC ad with the following code:
-
adView.startAutoRefresh();
-
adView.startAutoRefresh()
Manually refresh the contents with the following code. You must stop auto-refresh before you call loadAd()
.
-
adView.loadAd();
-
adView.loadAd()