Banner

Last Updated on: 19 Sep, 2023

Create Banner Ad

INFO

  • If you miss to Initialize the SDK, this method will fail to return a response.
  • Ensure that everything is executed on the main thread.

Follow the instructions below to create a Banner ad:

After your adunitId is generated, you can find it on the Meson UI. Add adunitId into the MesonBanner object and pass the ActivityContext.

Kotlin

val bannerAd = MesonBanner(this, "AD_UNIT_ID")

Java

MesonBanner bannerAd = new MesonBanner(this, "AD_UNIT_ID");

Set Banner Size

The banner object takes in the width and height of the banner.

  • For a banner, set 320 as the width and 50 as the height.
  • For an MREC banner, set 300 as the width and 250 as the height.

Kotlin

bannerAd.setBannerSize(AdSize(<width>,<height>))

Java

bannerAd.setBannerSize(new AdSize(<width>,<height>));

Additional Banner APIs

Additional APIs to provide more control over the banner ads.

Kotlin

bannerAd.disableHardwareAcceleration()   // Use this for android devices with RAM less than 1GB to reduce hardware resource usage

Java

bannerAd.disableHardwareAcceleration();   // Use this for android devices with RAM less than 1GB to reduce hardware resource usage

Register for Callbacks

Track the ad lifecycle for banner ads. All the available events for banner ads are listed below.

Kotlin

bannerAd.setAdListener(object: MesonBannerAdListener() { 
    override fun onAdImpression(ad: MesonBanner, mesonAdData: MesonAdData?) { 
    }
  
    override fun onAdExpanded(ad: MesonBanner) { 
    }
  
    override fun onAdCollapsed(ad: MesonBanner) { 
    }
  
    override fun onUserLeftApplication(ad: MesonBanner) { 
    }
  
    override fun onAdLoadSucceeded(ad: MesonBanner) { 
    }
  
    override fun onAdLoadFailed(ad: MesonBanner, status: MesonAdRequestStatus) { 
    }
  
    override fun onAdClicked(ad: MesonBanner, params: HashMap<String, Any>) { 
    } 
})

Java

bannerAd.setAdListener(new MesonBannerAdListener() { 
    @Override 
    public void onUserLeftApplication(MesonBanner mesonBanner) { 
        super.onUserLeftApplication(mesonBanner); 
    }
 
    @Override 
    public void onAdImpression(MesonBanner mesonBanner, @Nullable MesonAdData mesonAdData) { 
        super.onAdImpression(mesonBanner, mesonAdData); 
    }
 
    @Override 
    public void onAdClicked(MesonBanner mesonBanner, @NonNull HashMap<String, Object> hashMap) { 
        super.onAdClicked(mesonBanner, hashMap); 
    }
 
    @Override 
    public void onAdLoadFailed(MesonBanner mesonBanner, @NonNull MesonAdRequestStatus mesonAdRequestStatus) { 
        super.onAdLoadFailed(mesonBanner, mesonAdRequestStatus); 
    }
 
    @Override 
    public void onAdLoadSucceeded(MesonBanner mesonBanner) { 
        super.onAdLoadSucceeded(mesonBanner); 
    }
 
    @Override 
    public void onAdCollapsed(@NonNull MesonBanner mesonBanner) { 
        super.onAdCollapsed(mesonBanner); 
    }
  
    @Override 
    public void onAdExpanded(@NonNull MesonBanner mesonBanner) { 
        super.onAdExpanded(mesonBanner); 
    } 
});

Load and Show

You can request a banner ad by invoking this method from demand sources and selecting a winner. When the ad is available, it is shown to the user in the registered ad container.

Kotlin

adContainer.addView(bannerAd)
bannerAd.load()

Java

adContainer.addView(bannerAd); 
bannerAd.load();

Destroy Banner Ad

Use this method when the banner is no longer needed. We recommend using this onDestroy of the Activity.

Kotlin

bannerAd.destroy()

Java

bannerAd.destroy();