简体   繁体   中英

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 ?

xml:

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/bannerad"
    android:orientation="vertical"/>

Java code:

private MaxAdView adView;

private void LoadBannerAd() {
    adView = new MaxAdView( getResources().getString(R.string.bannerad), this );
    adView.setListener(new MaxAdViewAdListener() {
        @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) {

        }
    });

    int width = ViewGroup.LayoutParams.MATCH_PARENT;
    int heightPx = getResources().getDimensionPixelSize( R.dimen.banner_height );
    adView.setLayoutParams( new FrameLayout.LayoutParams( width, heightPx, Gravity.BOTTOM) );
    adView.setBackgroundColor(Color.BLACK);
    LinearLayout ll = findViewById(R.id.bannerad);
    ll.addView(adView);
    adView.loadAd();
}

Call the LoadBannerAd() inside your onCreate().

The problem is, you are using both the ways:

1: Using XML and java code
2: and programatically also.

Try the above code.

I know it's late to answer this question but may be this can help someone in future.

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 Inte.net 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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM