I'm using Admob AdView (adaptive banner), and I want to get the height of the banner. the banner defined like this:
private AdSize getAdSize() { //https://developers.google.com/admob/android/banner/adaptive
// Step 2 - Determine the screen width (less decorations) to use for the ad width.
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float widthPixels = outMetrics.widthPixels;
float density = outMetrics.density;
int adWidth = (int) (widthPixels / density);
// Step 3 - Get adaptive ad size and return for setting on the ad view.
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
mAdView = new AdView(getContext());
mAdView.setAdUnitId("00000000000000000000");
AdSize adSize = new AdSize(getAdSize().getWidth(), (int) ((getAdSize().getHeight()/3)*2.5));//I want the banner to be a bit smaller than returned.
mAdView.setAdSize(adSize);
List<String> testDeviceIds = Arrays.asList("00000000000000");
RequestConfiguration configuration =
new RequestConfiguration.Builder().setTestDeviceIds(testDeviceIds).build();
MobileAds.setRequestConfiguration(configuration);
//insert adView to wrap_content container
con.addView(mAdView);
adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
But the problem is:
this expression: (int) ((getAdSize().getHeight()/3)*2.5)
returns 52 in my case. but its not the real size of the banner. (he is bigger).
I tried to print banner's height on click and It turns out that he is actually 136px
.
Is it setAdSize()
bug? (or maybe it because the ad resolution)?
However , how can I get the real height before banner loaded? (without waiting for banner loading like getViewTreeObserver().addOnGlobalLayoutListener()
that not working either btw).
I tried using : mAdview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
and mAdView.getMeasuredHeight()
but it returns 0.
And I tried getHeight()
inside getViewTreeObserver().addOnGlobalLayoutListener()
and inside onAdLoaded()
(even that I want to know the height before loading) but it still 0.
In summary:
I have 2 questions:
52
as returned from (int) ((getAdSize().getHeight()/3)*2.5)
. so why setAdSize()
is faking values? (and how to avoid it)Thanks in advance.
The constructor for AdSize (int width, int height):
width: The width of the ad in density-independent pixels.
height: The height of the ad in density-independent pixels.