Ally
Ally

Reputation: 51

Admob SMART_BANNER size

The AdSize class has getWidthInPixels() and getHeightInPixels() methods to obtain size of the ad. Although, they work correctly for BANNER, they don't work SMART_BANNER. They always return -2.

Can you please suggest me a way to fetch AdView size during run-time?

Upvotes: 5

Views: 10267

Answers (1)

Eric Leichtenschlag
Eric Leichtenschlag

Reputation: 8931

If you're using the Google Play services library, you can simply use:

int widthPixels = AdSize.SMART_BANNER.getWidthInPixels(this);
int heightPixels = AdSize.SMART_BANNER.getHeightInPixels(this);

In the old standalone Android AdMob SDK, you have to do it the hacky way:

Disclaimer: This is the incorrect way to create an AdSize. Do NOT pass this AdSize into the AdView constructor!

Hopefully the smart banner implementation will be fixed in future versions so you don't have to do this hacky workaround. But here is how it can be done:

// This testSize should not be passed to the AdView constructor.
// Always pass AdSize.SMART_BANNER instead.
AdSize testSize = AdSize.createAdSize(AdSize.SMART_BANNER, this);
int widthPixels = testSize.getWidthInPixels(this);
int heightPixels = testSize.getHeightInPixels(this);
// testSize should not be referenced past this point.

Upvotes: 19

Related Questions