Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...