Sandeep Sandeep - 21 days ago 8
Android Question

Admob banner size for different devices

Just two days back, I came to know that SMART_BANNER is not the best for good CTR and we should dynamically switch between ad sizes for admob.

Here is the Java code, I have written. When I ran the code on a 4inch emulator, I see that 728x90 ad is requested and the response is invalid ad size. (description of error is that ad won't fit the current screen) pls. help:

AdSize adsize = AdSize.SMART_BANNER;

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int orientation = display.getOrientation();

if(width >= 728 && height >= 90 ) {
adsize = AdSize.IAB_LEADERBOARD;
System.out.println("728 x 90");
} else if (width >= 468 && height >= 60 ) {
adsize = AdSize.IAB_BANNER;
System.out.println("468 x 60");
} else if (width >= 320 && height >= 50 ) {
adsize = AdSize.BANNER;
System.out.println("320 x 50");
}

LinearLayout adContainer = (LinearLayout) findViewById(R.id.cakes);
adView = new AdView(this, adsize, "xxxxxxxxxx");
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);

// Place the ad view.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
adContainer.addView(adView, params);

Answer Source

getWindowManager().getDefaultDisplay().getWidth() returns the width in pixels. You need to be making a decision on which banner to show based upon the width in density independent pixels.

I decided a long time ago that the best solution was to use Android resource config to specify the adBannerSize. Eg

final AdSize adSize;
final int adBannerSize = getResources().getInteger(R.integer.adBannerSize);
switch (adBannerSize) {
    case 1 :
        adSize = AdSize.BANNER;
        break;
    case 2 :
        adSize = AdSize.IAB_BANNER;
        break;
    case 3 :
        adSize = AdSize.IAB_LEADERBOARD;
        break;
    default:
        Log.w(TAG, "No AdSize specified");
        adSize = AdSize.BANNER;
        break;
}

Then you can easily configure the ad banner size to match whatever device configuration you intend to support.