androidkotlinadmobbanner-ads

Android Kotlin - AdMob adaptive banner getCurrentOrientationAnchoredAdaptiveBannerAdSize


As in the official Google docs, which are as always incomplete, I try to use this code:

private AdSize getAdSize() {
  // Determine the screen width (less decorations) to use for the ad width.
  Display display = getWindowManager().getDefaultDisplay();
  DisplayMetrics outMetrics = new DisplayMetrics();
  display.getMetrics(outMetrics);

  float density = outMetrics.density;

  float adWidthPixels = adContainerView.getWidth();

  // If the ad hasn't been laid out, default to the full screen width.
  if (adWidthPixels == 0) {
    adWidthPixels = outMetrics.widthPixels;
  }

  int adWidth = (int) (adWidthPixels / density);
  return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}

what is getCurrentOrientationAnchoredAdaptiveBannerAdSize suppose to be??

I get Unresolved reference on it


Solution

  • If you read the documentation, you can learn that:

    Returns an AdSize with the given width and a Google-optimized height to create a banner ad. The size returned will have an aspect ratio similar to AdSize, suitable for anchoring near the top or bottom of your app. The height will never be larger than 15% of the device's current orientation height and never smaller than 50px. This function always returns the same height for any width / device combination. If the context is null or the device height cannot be determined from the context, INVALID is returned.

    It's purpose is to allow the banner's height to be dynamic without setting a specific height. I don't know what stands behind " a Google-optimized height", but there is enough in the explanation to give you a ballpark understanding.

    Also, in the same documentation page, you can see that this method is suggested to be used instead of the field of Smart Banner as it is deprecated.