AppLovin MAX Banner Ads Integration Guide (Android)

AppLovin MAX Banner Ads

This tutorial is a part of the AppLovin ads integration guide. To show MAX banner ads, we need to create ad units first. Login to your AppLovin dashboard, go to MAX > Ad Units section on the left panel. Next, click on the “Create Ad Unit” button.

Create Ad Unit

On the next page, type a name for the ad unit. Select the Android platform and type your app name. Your app will come up in the list. Next, select Banner ad type. We will add the mediation networks later. Scroll down and select a refresh interval. Next, click on the green color Save button.

AppLovin MAX Banner ad

It creates an ad unit along with a unique id. Copy the id.

Next, we need to set different banner heights for mobile and tablet devices. Expand the project pane in the Android Studio. Under the res folder, right-click on the values folder > New > Values Resource File.

Ad Unit Height

Type attrs in the file name section and click on the OK button.

Now, open the attrs.xml, remove the existing code, and paste the following code.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="banner_height">50dp</dimen>
</resources>

Next, right-click on the same values folder > New > Values Resource File. Type attrs in the file name section. This time, select “Smallest screen width” in the “Available qualifiers” section, and click on the >> button. Type 600 in the “Smallest screen width” box. Next, click on the OK button.

Banner Ad Unit Height Tablet

Now, open the attrs.xml (sw600dp), remove the existing code, and paste the following code. This is for tablet devices.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="banner_height">90dp</dimen>
</resources>

Now, let’s start coding. There are different ways to display the banner ad.

  1. Using XML layout
  2. Using Java/Kotlin code
  3. Banner Ad in Jetpack Compose

1. Using XML Layout:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.applovin.mediation.ads.MaxAdView xmlns:maxads="http://schemas.applovin.com/android/1.0"
        android:id="@+id/ad_view_banner"
        android:layout_width="0dp"
        android:layout_height="@dimen/banner_height"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        maxads:adUnitId="YOUR_AD_UNIT_ID" />

</androidx.constraintlayout.widget.ConstraintLayout>

Don’t forget to replace YOUR_AD_UNIT_ID with the actual id.

MainActivity.kt (kotlin):

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdViewAdListener
import com.applovin.mediation.MaxError
import com.applovin.mediation.ads.MaxAdView
import com.applovin.sdk.AppLovinSdk
import com.applovin.sdk.AppLovinSdkConfiguration

class MainActivity : AppCompatActivity(), MaxAdViewAdListener {

    private var adViewBanner: MaxAdView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        adViewBanner = findViewById(R.id.ad_view_banner)
        initializeAdNetwork() // initialize ads only once during the app startup
        loadBannerAd()
    }

    private fun initializeAdNetwork() {
        AppLovinSdk.getInstance(getApplicationContext()).mediationProvider = "max"
        AppLovinSdk.getInstance(getApplicationContext()).initializeSdk { configuration: AppLovinSdkConfiguration ->
              
        }
    }

    private fun loadBannerAd() {
        adViewBanner?.setListener(this)
        adViewBanner?.visibility = View.VISIBLE
        adViewBanner?.loadAd()
        adViewBanner?.startAutoRefresh()
    }

    override fun onDestroy() {
        // Destroy the ad
        adViewBanner?.stopAutoRefresh()
        adViewBanner?.destroy()
        adViewBanner = null
        super.onDestroy()
    }

    // The following methods are MAX ad listeners

    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 */ }
}

MainActivity.java (Java):

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdViewAdListener;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.ads.MaxAdView;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;

public class MainActivity extends AppCompatActivity implements MaxAdViewAdListener {

    private MaxAdView adViewBanner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adViewBanner = findViewById(R.id.ad_view_banner);
        initializeAdNetwork(); // initialize ads only once during the app startup
        loadAd();
    }

    private void initializeAdNetwork() {
        AppLovinSdk.getInstance(getApplicationContext()).setMediationProvider("max");
        AppLovinSdk.initializeSdk(getApplicationContext(), new AppLovinSdk.SdkInitializationListener() {
            @Override
            public void onSdkInitialized(final AppLovinSdkConfiguration configuration) {
            }
        });
    }

    private void loadAd() {
        adViewBanner.setListener(this);
        adViewBanner.loadAd();
        adViewBanner.startAutoRefresh();
    }

    @Override
    protected void onDestroy() {
        if (adViewBanner != null) {
            adViewBanner.stopAutoRefresh();
            adViewBanner.destroy();
        }
        super.onDestroy();
    }

    @Override
    public void onAdExpanded(MaxAd ad) {
    }

    @Override
    public void onAdCollapsed(MaxAd ad) {
    }

    @Override
    public void onAdLoaded(MaxAd ad) {
    }

    @Override
    public void onAdDisplayed(MaxAd ad) {
    }

    @Override
    public void onAdHidden(MaxAd ad) {
    }

    @Override
    public void onAdClicked(MaxAd ad) {
    }

    @Override
    public void onAdLoadFailed(String adUnitId, MaxError error) {
    }

    @Override
    public void onAdDisplayFailed(MaxAd ad, MaxError error) {
    }
}

2. Using Java/Kotlin code:

If you don’t like XML, here is the Kotlin and Java code.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/main_root_view"
    tools:context=".MainActivity">

</LinearLayout>

MainActivity.kt (Kotlin):

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdViewAdListener
import com.applovin.mediation.MaxError
import com.applovin.mediation.ads.MaxAdView
import com.applovin.sdk.AppLovinSdk
import com.applovin.sdk.AppLovinSdkConfiguration

class MainActivity : AppCompatActivity(), MaxAdViewAdListener {

    private var adView: MaxAdView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initializeAdNetwork() // initialize ads only once during the app startup
        createBannerAd()
        loadBannerAd()
    }

    private fun initializeAdNetwork() {
        AppLovinSdk.getInstance(getApplicationContext()).mediationProvider = "max"
        AppLovinSdk.getInstance(getApplicationContext()).initializeSdk { configuration: AppLovinSdkConfiguration ->
              
        }
    }

    private fun createBannerAd() {
        adView = MaxAdView("YOUR_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(ContextCompat.getColor(applicationContext, R.color.white))

        val rootView = findViewById<ViewGroup>(R.id.main_root_view)
        rootView.addView(adView)
    }

    private fun loadBannerAd() {
        adView?.loadAd()
        adView?.startAutoRefresh()
    }

    override fun onDestroy() {
        // Destroy the ad
        adView?.stopAutoRefresh()
        adView?.destroy()
        adView = null
        super.onDestroy()
    }

    // The following methods are 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 */ }
}

MainActivity.java (Java):

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdViewAdListener;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.ads.MaxAdView;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;

public class MainActivity extends AppCompatActivity implements MaxAdViewAdListener {

    private MaxAdView adViewBanner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initializeAdNetwork(); // initialize ads only once during the app startup
        createBannerAd();
    }

    private void initializeAdNetwork() {
        AppLovinSdk.getInstance(getApplicationContext()).setMediationProvider("max");
        AppLovinSdk.initializeSdk(getApplicationContext(), new AppLovinSdk.SdkInitializationListener() {
            @Override
            public void onSdkInitialized(final AppLovinSdkConfiguration configuration) {
            }
        });
    }

    void createBannerAd() {
        adViewBanner = new MaxAdView("YOUR_AD_UNIT_ID", this);
        adViewBanner.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);

        adViewBanner.setLayoutParams(new FrameLayout.LayoutParams(width, heightPx));

        // Set background or background color for banners to be fully functional
        adViewBanner.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.white));

        ViewGroup rootView = findViewById(android.R.id.content);
        rootView.addView(adViewBanner);

        loadAd();
    }

    private void loadAd() {
        adViewBanner.loadAd();
        adViewBanner.startAutoRefresh();
    }

    @Override
    protected void onDestroy() {
        if (adViewBanner != null) {
            adViewBanner.stopAutoRefresh();
            adViewBanner.destroy();
        }
        super.onDestroy();
    }

    @Override
    public void onAdExpanded(MaxAd ad) {
    }

    @Override
    public void onAdCollapsed(MaxAd ad) {
    }

    @Override
    public void onAdLoaded(MaxAd ad) {
    }

    @Override
    public void onAdDisplayed(MaxAd ad) {
    }

    @Override
    public void onAdHidden(MaxAd ad) {
    }

    @Override
    public void onAdClicked(MaxAd ad) {
    }

    @Override
    public void onAdLoadFailed(String adUnitId, MaxError error) {
    }

    @Override
    public void onAdDisplayFailed(MaxAd ad, MaxError error) {
    }
}

3. Banner Ad in Jetpack Compose:

There is no dedicated composable to display banner ads in Jetpack Compose. But, Jetpack Compose offers AndroidView. It helps us to display classic Android Views. We can use it for our banner ad.

MainActivity.kt:

import android.os.Bundle
import android.view.View
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.viewinterop.AndroidView
import com.applovin.mediation.ads.MaxAdView
import com.applovin.sdk.AppLovinPrivacySettings
import com.applovin.sdk.AppLovinSdk

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        initializeAdNetworks()

        setContent {
            YourProjectNameTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Box(
                        modifier = Modifier.fillMaxSize()
                    ) {

                        Column(
                            modifier = Modifier
                                .padding(bottom = dimensionResource(R.dimen.banner_height))
                                .fillMaxSize()
                        ) {
                            // your UI here
                        }

                        // MAX banner ad
                        AndroidView(
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(height = dimensionResource(R.dimen.banner_height))
                                .align(alignment = Alignment.BottomCenter),
                            factory = { context ->
                                MaxAdView("YOUR_AD_UNIT_ID", context).apply {
                                    visibility = View.VISIBLE
                                    loadAd()
                                    startAutoRefresh()
                                }
                            }
                        )
                    }
                }
            }
        }
    }

    private fun initializeAdNetworks() {

        AppLovinPrivacySettings.setHasUserConsent(false, applicationContext)
        AppLovinPrivacySettings.setIsAgeRestrictedUser(false, applicationContext)
        AppLovinPrivacySettings.setDoNotSell(true, applicationContext)

        //AdSettings.setDataProcessingOptions(arrayOf<String>())

        AppLovinSdk.getInstance(this).mediationProvider = "max"
        AppLovinSdk.getInstance(this).initializeSdk {
            // AppLovin SDK is initialized, start loading ads
        }
    }
}

Don’t forget to replace YOUR_AD_UNIT_ID with the actual id.

Now, run the app. You will see a test ad. If nothing shows up, check the following things:

  • AppLovin SDK key in the manifest file.
  • See the network you selected in the Test Device section. You should add the network’s dependency in the build.gradle file.
  • Check if the YOUR_AD_UNIT_ID is correct and belongs to the app you are testing.

If you still have problem, look at the onAdLoadFailed() and onAdDisplayFailed() methods. You have access to the error object. Log the error.getMessage() to see the problem.

If you want to display AdMob adaptive banner ads, follow this guide.

This is about MAX banner ads integration in Android. Hopefully, you are seeing the ads without errors. If you have any problem, comment below.

4 thoughts on “AppLovin MAX Banner Ads Integration Guide (Android)”

Leave a Comment