Back to top

Banner Ad Units

Last Updated on: 13 Jul, 2022

Create Banner Ad Unit

INFO

If you miss to Initialize the SDK, this method will fail to return a response.

To create a new banner ad, create a MesonBanner object.

Kotlin

val bannerAd = MesonBanner(BannerAdsActivity.this, "AD_UNIT_ID")

Java

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

Set Banner Size

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

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 
bannerAd.destroy()  // Destroys the banner ad object

Java

bannerAd.disableHardwareAcceleration();   // Use this for android devices with RAM less than 1GB to reduce hardware resource usage 
bannerAd.destroy();  // Destroys the banner ad object

Register for Postbacks

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(bannerAd: MesonBanner, mesonAdData: MesonAdData?)) {
    }

    override fun onAdExpanded(bannerAd: MesonBanner) {
    }

    override fun onAdCollapsed(bannerAd: MesonBanner) {
    }

    override fun onUserLeftApplication(bannerAd: MesonBanner) {
    }

    override fun onAdLoadSucceeded(bannerAd: MesonBanner) {
    }

    override fun onAdLoadFailed(bannerAd: MesonBanner, status: MesonAdRequestStatus) {
    }

    override fun onAdClicked(bannerAd: MesonBanner, params: HashMap<String, Any>) {
    }
}

Java

bannerAd.setAdListener(new MesonBannerAdListener() {
  @Override
  public void onAdLoadSucceeded(MesonBanner bannerAd) {
      Log.d("", "onAdLoadSucceeded with bid ");
  }

  @Override
  public void onAdExpanded(@NonNull MesonBanner bannerAd) {
      super.onAdExpanded(bannerAd);
  }

  @Override
  public void onAdCollapsed(@NonNull MesonBanner bannerAd) {
      super.onAdCollapsed(bannerAd);
  }

  @Override
  public void onAdClicked(MesonBanner bannerAd, @NonNull HashMap<String, Object> hashMap) {
      super.onAdClicked(bannerAd, hashMap);
  }

  @Override
  public void onAdImpression(MesonBanner bannerAd, @Nullable MesonAdData mesonAdData) {
      super.onAdImpression(bannerAd, impressionData);
  }

  @Override
  public void onAdLoadFailed(MesonBanner bannerAd, MesonAdRequestStatus mesonAdRequestStatus) {
      Log.d("", "Banner ad failed to load with error: " + mesonAdRequestStatus.getMessage());
  }

  @Override
  public void onUserLeftApplication(MesonBanner bannerAd) {
      Log.d("", "onUserLeftApplication");
  }
});

Load and Show

The publisher can request for a banner ad by invoking this method. This will request an ad from all demand sources, select a winner, and once the ad is available show the ad to the user in the registered ad container.

Kotlin

adContainer.addView(bannerAd) 
bannerAd.load()

Java

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