Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get banner size of smart banner?

I'm trying to integrate admob via firebase_admob into my flutter app.

By default, it seems to just overlay/stack the banner on top of the current view ... without respecting the actual widget tree and it's constraints.

I have really no idea why somebody would design a library like that .. but ok. Maybe for anti-fraud reasons?!

To avoid covering actual content with the banner, I would like to add a padding (padding height = banner-height) to my content.

Since I'm using 'smart-banners', the library dynamically tries to find the best banner size for the given screen size at run-time.

How to find out what banner size it came up with??

like image 831
forgemo Avatar asked Jun 19 '18 19:06

forgemo


People also ask

How do you make a smart banner?

Create an ad unit with the Smart Banner sizeCreate an ad unit as you normally would and select Smart Banner from the “Size mode” drop-down. In the app, use one of the following ad sizes for the ad view: iOS: kGADAdSizeSmartBannerPortrait or kGADAdSizeSmartBannerLandscape. Android: SMART_BANNER.

What is a smart banner?

Smart Banners are ad units that render screen-width banner ads on any screen size across different devices in either orientation. Smart Banners detect the width of the device in its current orientation and create the ad view that size. Three ad heights are implemented in smart banners: Ad height.

What is Banner Admob?

Banner ad units display rectangular ads that occupy a portion of an app's layout. They stay on screen while users are interacting with the app, either anchored at the top or bottom of the screen or inline with content as the user scrolls. Banner ads can refresh automatically after a certain period of time.

What are banners on Android?

Banner ads occupy a spot within an app's layout, either at the top or bottom of the device screen. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. If you're new to mobile advertising, they're a great place to start.


1 Answers

This is a code I'm using to get the height of a smartbanner, according to Google Specs.

  /// The initial size of the banner is calculated on the height of the
  /// viewport. Due to ADMob banner refresh policies, in order to have
  /// a consistent behaviour, we should keep track of the current AD size
  /// and maintain it when the user rotates the screen, and update that
  /// value at every banner successful.
  /// For now, we will avoid this complexity and set the banner height to
  /// the maximum height that a banner could get on this device, forcing
  /// the use of the longest side as the base.
  /// see https://developers.google.com/admob/android/banner#smart_banners
  double _getSmartBannerHeight(BuildContext context) {
    MediaQueryData mediaScreen = MediaQuery.of(context);
    double dpHeight = mediaScreen.orientation == Orientation.portrait
        ? mediaScreen.size.height
        : mediaScreen.size.width;
    log.fine("Device height: $dpHeight");
    if (dpHeight <= 400.0) {
      return 32.0;
    }
    if (dpHeight > 720.0) {
      return 90.0;
    }
    return 50.0;
  }

As you can see, instead of calculate the banner's height in relation to the actual device's height (in other words according to device orientation) I'm using always the longest side of the device.

This is done because when the user rotates the device, the official flutter_admob plugin does not reload an appropriate banner. Consider this scenario: - device in portrait, banner loads at 50 - user rotate the device: according to docs, the height should be 32, but the banner will not be reloaded, leaving it at height 50 - a new banner load occurs, according to your campaign settings, providing a banner with height of 32 - user rotates again the device and so on

As I stated in the comment, an effective resolution would be to track down the last banner size, and react to the banner load events in order to provide a seamless experience for the user.

like image 69
Mattia Galati Avatar answered Oct 14 '22 10:10

Mattia Galati