Admob banner size for different devices

2019-04-02 07:32发布

问题:

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);

回答1:

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.



回答2:

Since V6.0 , You can use the constante SMART_BANNER as AdSize and ADS will display differently depending on screen size.



回答3:

I had the same issue but my ads do not fill the full screen width, so I couldn't rely on screen size or resource qualifiers.

I found the solution after realizing that the AdSize documentation states that the size must be specified in density-independent pixels. My final code:

int width = container.getWidth();
width /= getResources().getDisplayMetrics().density;
int height = 50;
if(width >= 728)
   height = 90;
else if(width >= 468)
   height = 60;

return new AdSize(width, height);


标签: android admob