Loading and Showing Banners Programmatically
To load a banner, create a MaxAdView
object that corresponds to your ad unit and then call its 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 and of other ad-related events.
-
public class ExampleActivity extends Activity implements MaxAdViewAdListener { private MaxAdView adView; void createBannerAd() { 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 ); // 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 createBannerAd() { 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 or background color for banners 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 Banners in Layout Editor
Alternatively, you can add MAX banners to your view layout XML. Ensure that your banners are fully functional by setting a background or background color (android:background) and by stretching the width (android:layout_width) to the width of the screen:
<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"?> <resources> <dimen name="banner_height">50dp</dimen> </resources>
Declare the tablet banner height of 90dp in res/values-sw600dp/attrs.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="banner_height">90dp</dimen> </resources>
You still must call loadAd()
on the MaxAdView
you create in this way:
-
MaxAdView adView = findViewById( R.id.ad_view ); adView.loadAd();
-
var adView: MaxAdView = findViewById(R.id.ad_view) adView.loadAd()
Destroying Banners
After you are no longer using a banner ad, you must clean up its resources by calling destroy()
. If you do not do this, the performance of your app will degrade.
-
adView.destroy();
-
adView.destroy()
Adaptive Banners
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 to reduce the likelihood of accidental clicks. 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 width of the banner. You integrate adaptive banners in a similar way to how you integrate regular banners, except that you must set the height to the value returned by MAAdFormat.banner.adaptiveSize.height
instead of 50 or 90. Optionally, for more specific integrations you can set a custom width (in DP) 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. Before you load the ad, set the banner extra parameter adaptive_banner
to true
as in the code below:
-
void createBannerAd() { 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" ); adView.setLocalExtraParameter( "adaptive_banner_width", 400 ); adView.getAdFormat().getAdaptiveSize( 400, context ).getHeight(); // Set your ad height to this value // Set background or background color for banners to be fully functional adView.setBackgroundColor( … ); ViewGroup rootView = findViewById( android.R.id.content ); rootView.addView( adView ); // Load the ad adView.loadAd(); }
-
fun createBannerAd() { 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") adView.setLocalExtraParameter("adaptive_banner_width", 400) adView.adFormat.getAdaptiveSize(400, context).height // Set your ad height to this value // Set background or background color for banners to be fully functional adView?.setBackgroundColor(…) val rootView = findViewById<ViewGroup>(android.R.id.content) rootView.addView(adView) // Load the ad adView?.loadAd() }
If you need to do any UI configurations that depend on the adaptive banner size, you can retrieve the width and height of the loaded ad, in DP, as follows:
-
@Override 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 }
Stopping and Starting Auto-Refresh
There may be cases when you would like to stop auto-refresh, for instance, when you hide a banner ad or want to manually refresh. Stop auto-refresh for a banner 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 a banner 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()