Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Admob banner size for different devices

Tags:

android

admob

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);
like image 911
Piyush-Ask Any Difference Avatar asked Dec 12 '25 09:12

Piyush-Ask Any Difference


1 Answers

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.

like image 109
William Avatar answered Dec 14 '25 07:12

William



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!