Android – AdMob Smart Banner Mediation Workaround

Background

Using the AdMob mediation network has been very convenient. There is a newer mobile ad banner type called “Smart Banners“, which AdMob created, that allows the SDK to automatically display the best banner size based on the device’s screen resolution. The Android device ecosystem has thousands of different devices with a wide variety of screen sizes. Programmatically determining the best banner size is very convenient.

Issue

While AdMob supports the Smart Banner ad type, some other ad networks do not. When using AdMob mediation, if the Smart Banner ad type is passed through to another ad network, which doesn’t support Smart Banners, then no ad will be displayed. For example, a common AdMob mediation ad partner, InMobi, does not support the Smart Banner ad type.

Solution

Create a “Smart Banner” function that determines the layout size available on the device and adjusts the ad banner request.
Millennial Media has a great function called canFit, which helps determine the best sized ad for the device. Using this function, we can emulate the Smart Banner functionality across all mediation ad networks, regardless of whether they’ve added support for the Smart Banner ad type.

//Constants for tablet sized ads (728x90)
//AdMob name: LEADERBOARD
final int IAB_LEADERBOARD_WIDTH = 728;
final int IAB_LEADERBOARD_HEIGHT = 90;
//AdMob name: FULL_BANNER
final int MED_BANNER_WIDTH = 480;
final int MED_BANNER_HEIGHT = 60;
//Constants for phone sized ads (320x50)
//AdMob name: BANNER
final int BANNER_AD_WIDTH = 320;
final int BANNER_AD_HEIGHT = 50;
 
//Finds an ad that best fits a user's device.
LinearLayout.LayoutParams adViewLayoutParams = null;
if(canFit(activity, IAB_LEADERBOARD_WIDTH)) {
    adViewLayoutParams = new LinearLayout.LayoutParams((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, IAB_LEADERBOARD_WIDTH, activity.getResources().getDisplayMetrics()),
            (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, IAB_LEADERBOARD_HEIGHT, activity.getResources().getDisplayMetrics()));
    adViewLayoutParams.gravity = Gravity.CENTER;
    adView.setAdSize(AdSize.LEADERBOARD);
} else if(canFit(activity, MED_BANNER_WIDTH)) {
    adViewLayoutParams = new LinearLayout.LayoutParams((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MED_BANNER_WIDTH, activity.getResources().getDisplayMetrics()),
            (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MED_BANNER_HEIGHT, activity.getResources().getDisplayMetrics()));
    adViewLayoutParams.gravity = Gravity.CENTER;
    adView.setAdSize(AdSize.FULL_BANNER);
} else {
    adViewLayoutParams = new LinearLayout.LayoutParams((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, BANNER_AD_WIDTH, activity.getResources().getDisplayMetrics()),
            (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, BANNER_AD_HEIGHT, activity.getResources().getDisplayMetrics()));
    adViewLayoutParams.gravity = Gravity.CENTER;
    adView.setAdSize(AdSize.BANNER);
}
 
//Method from MillenialMedia
private static boolean canFit(Activity activity, int adWidth) {
    int adWidthPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, adWidth, activity.getResources().getDisplayMetrics());
    DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
    return metrics.widthPixels >= adWidthPx;
}

The code is also available on a GitHub gist: https://gist.github.com/joeykrim/5dbd7a685b8d057182e4

Sources

AdMob Smart Banners
MillenialMedia Android SDK

Leave a Reply

Your email address will not be published. Required fields are marked *

*