androidbannerbanner-adsapplovin

How to show Applovin/Max Banner Ad with layout?


I am new to Applovin/Max. In their documentation position and size of Banner ad set by programmatically. Thats why BannerAd override my app's content.

Banner layout in xml

<com.applovin.mediation.ads.MaxAdView
        android:id="@+id/maxBannerAdLayout"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="@dimen/banner_height" />

In documentation banner show progrmatically

adView = new MaxAdView( "YOUR_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 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();

I have no idea, How can I connect code with banner layout?


Solution

  • Note: When you create Ad Id in Applovin/Max that time you provide package Name. Only Test ads on that same package name app otherwise not showing any real ad.

    Here I explain Full process of showing Max/Applovin Banner ad (with fb/Meta).

    add dependency in build.gradle of applovin and fb.(if you use other network then add their dependency with applovin. (always check latest dependency version- here)

    //applovin and fb dependencies
    implementation 'com.applovin:applovin-sdk:11.6.1'
    implementation 'com.applovin.mediation:facebook-adapter:6.12.0.2'
    implementation 'com.applovin.mediation:facebook-adapter:6.12.0.2'
    implementation 'com.facebook.android:audience-network-sdk:6.12.0'
    

    add this code me manifests.xml bottom. Here in value put your sdk key you can find it in Applovin >Account > Keys > sdk Key

          ....
        <meta-data
            android:name="applovin.sdk.key"
            android:value="@string/sdkKey" />
    </application>
    

    Place Banner ad code om xml where you want

    <com.applovin.mediation.ads.MaxAdView
            xmlns:maxads="http://schemas.applovin.com/android/1.0"
            maxads:adUnitId="Add_here_YOUR_AD_ID"
            android:background="@color/white"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/maxBannerADDDD"
            />
    

    Java code

        private MaxAdView adView;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //find Id of xml Banner 
        adView = (MaxAdView) findViewById(R.id.bannerADDDD);
        initializeAdNetwork();
    }
    
    private void initializeAdNetwork() {
        
        AppLovinSdk.getInstance( this ).setMediationProvider( "max" );
        AppLovinSdk.initializeSdk(getApplicationContext(), new AppLovinSdk.SdkInitializationListener() {
            public void onSdkInitialized(AppLovinSdkConfiguration appLovinSdkConfiguration) {
                loadInterstitial();
    
                //load banner Ad
                loadBannerAd();
            }
        });
    
    
    
    }
    
    private void loadBannerAd(){
        adView.loadAd();
        adView.startAutoRefresh();
    }
    

    Also add Internet permission in app.

    If you use facebook/Meta ads then must use network_security_config. make this xml inside res > xml (if you not find xml then Right click on res > new > create Directory named xml) inside this xml directory create this below xml file.

    network_security_config.xml

        <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">127.0.0.1</domain>
        </domain-config>
    </network-security-config>
    

    also declare this file in menifest.xml

        ...
    <application 
      
        android:networkSecurityConfig="@xml/network_security_config" 
        ....
    

    Thanks.

    Happy Coding:)