0

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:

  1. I can see that the banner's height is not 52 as returned from (int) ((getAdSize().getHeight()/3)*2.5). so why setAdSize() is faking values? (and how to avoid it)
  2. And how can I get the real shown height?

Thanks in advance.

ATP
  • 2,939
  • 4
  • 13
  • 34
  • Take a look at this: https://stackoverflow.com/a/28136027/878126 Try to implement and use runJustBeforeBeingDrawn() method – Orest Fufalko Jan 12 '21 at 20:12
  • Not working either. return 0. – ATP Jan 13 '21 at 05:58
  • Why are you using `(int) ((getAdSize().getHeight()/3)*2.5)`? Shouldn't you be using the DPI <-> Pixel calculation: https://stackoverflow.com/a/61910409/295004 ? – Morrison Chang Jan 13 '21 at 07:21
  • the dimensions of `AdSise()` constructor is in `dp`? – ATP Jan 13 '21 at 07:43
  • ohhh I'm feeling so dumb. thank you so much!! @MorrisonChang post an answer for winning the bounty docs: > `width: The width of the ad in density-independent pixels. height: The height of the ad in density-independent pixels.` – ATP Jan 13 '21 at 18:28

2 Answers2

3

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.

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
2

Finally I found way to fix the getViewTreeObserver().addOnGlobalLayoutListener() way by call it after ad loaded:

mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                //mAdView.getHeight() returns 0 since the ad UI didn't load
                mAdView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() { 
                        mAdView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        //mAdView.getHeight() works here
                    }
                });
            }
            
 });

And as Morrison Chang said the width/height values for AdSize constructor should be specified in dp not in px:

width: The width of the ad in density-independent pixels.
height: The height of the ad in density-independent pixels.

ATP
  • 2,939
  • 4
  • 13
  • 34