android obtiene la altura del banner de adMob cuando aparece

I am adding an adMob banner to my app successfully. When banner appears I need to get its height in order to resize all layout elements. I am using the event onReceivedAd, that is properly fired. However, alturaBanner is = 0. Then, how to get its height? thank you.

   /** Called when an ad is received. */
    @Override
    public void onReceiveAd(Ad ad) 
    {
        adView.setVisibility(View.VISIBLE);

        int alturaBanner = adView.getHeight();

        RelativeLayout.LayoutParams params1 = (android.widget.RelativeLayout.LayoutParams) browse2
        .getLayoutParams();

        params1.setMargins(0, alturaBanner, 0, 0);

      Log.d(LOG_TAG, "onReceiveAd");
      Toast.makeText(this, "onReceiveAd", Toast.LENGTH_SHORT).show();
    }

preguntado el 03 de mayo de 12 a las 15:05

5 Respuestas

You can get the height of any type of banner before it is even added to the layout.

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

or

int heightPixels = AdSize.FULL_BANNER.getHeightInPixels(myContext);

or for DIP's

int heightDP = AdSize.BANNER.getHeight();

So for your need, you could do this:

/** Called when an ad is received. */
@Override
public void onReceiveAd(Ad ad) 
{
    adView.setVisibility(View.VISIBLE);

    int alturaBanner = AdSize.BANNER.getHeight(); // This gets the adsize, even if the view is not inflated. 

    RelativeLayout.LayoutParams params1 = (android.widget.RelativeLayout.LayoutParams) browse2
    .getLayoutParams();

    params1.setMargins(0, alturaBanner, 0, 0);

  Log.d(LOG_TAG, "onReceiveAd");
  Toast.makeText(this, "onReceiveAd", Toast.LENGTH_SHORT).show();
}

Solo cambia AdSize.BANNER a AdSize.SMART_BANNER or whatever banner type your using.

Add Sizes Get Height

Respondido el 10 de diciembre de 18 a las 05:12

As of now, AdSize.SMART_BANNER.getHeight(this); is not valid, and getHeight() is returning nonsense values (-2 in my case). - Fran Marzoa

AdSize.SMART_BANNER.getHeightInPixels(context) works like a charm so I can adjust my layout at runtime when the ad is loaded. The inner code in that function is in fact the same calculation shown here for smart banners stackoverflow.com/a/53061092/513479 - txedo

getting the height of the view before it was prepared will always return you 0 . use the next code in order to get its correct size , no matter which device/screen you have:

private static void runJustBeforeBeingDrawn(final View view, final Runnable runnable)
{
    final ViewTreeObserver vto = view.getViewTreeObserver();
    final OnPreDrawListener preDrawListener = new OnPreDrawListener()
    {
        @Override
        public boolean onPreDraw()
        {
            runnable.run();
            final ViewTreeObserver vto = view.getViewTreeObserver();
            vto.removeOnPreDrawListener(this);
            return true;
        }
    };
    vto.addOnPreDrawListener(preDrawListener);
}

inside the given runnable , you can query the real size of the view.

alternatively , you can use addOnGlobalLayoutListener instead of addOnPreDrawListener if you wish.

otro enfoque es usar onWindowFocusChanged (y verificar que hasFocus==true), pero esa no siempre es la mejor manera (solo use para la creación de vistas simples, no para creaciones dinámicas)

EDITAR: Alternativa a runJustBeforeBeingDrawn: https://stackoverflow.com/a/28136027/878126

Respondido 10 Jul 18, 11:07

previous answer work for me but also this method could be applied! thanks, +1 - Jaume

previous answer might not work on some devices and also possible future devices . my answer is more generic and will work for any view , and for any ad framework you use - if you ever choose a different ad framework , it will still work for you . - desarrollador de Android

I think this answer is not only better than the other one, it is also more acurated given the original question, that was "how to get adMob banner height". - Fran Marzoa

yes , an alternative would be to use "addOnGlobalLayoutListener" . - desarrollador de Android

@androiddeveloper i'm facing a serious problem. When ads appear i.e adView.setVisibilit(View.Visible). The views in my fragment that i have added programmatically gets disappeared (may be destroyed) any idea? - muhammad babar

I use the following method to get AdView's height:

adView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int height = adView.getHeight();
        if (height > 0) {
            // now the height is gotten, you can do things you want
        }
    }
});

onGlobalLayout() is triggered when the global layout state or the visibility of views within the view tree changes.

Respondido 04 Oct 13, 10:10

Actually, you don't need to wait for appearance of adview to get adMob banner height if you are using smart banner type for showing banner ads.

Use Smart banner as it automatically decides the height of ad based on device size. Use full width of screen to show the ad.

From android developers web oficial:

Smart Banners are ad units that will render screen-wide banner ads on any screen size across different devices in either orientation. Smart Banners help deal with increasing screen fragmentation across different devices by "smartly" detecting the width of the phone in its current orientation, and making the ad view that size.

Three ad heights (in dp, density-independent pixel) are available:

32 - used when the screen height of a device is less than 400
50 - used when the screen height of a device is between 400 and 720 90 - used when the screen height of a device is greater than 720

Now, get the height of AdView and adjust the margin of the layout where you wish to place the banner ad. Once the ad is loaded (by overriding on onAdLoaded API), you know the height using below method:

public static int getAdViewHeightInDP(Activity activity) {
    int adHeight = 0;

    int screenHeightInDP = getScreenHeightInDP(activity);
    if (screenHeightInDP < 400)
        adHeight = 32;
    else if (screenHeightInDP <= 720)
        adHeight = 50;
    else
        adHeight = 90;

    return adHeight;
}

public static int getScreenHeightInDP(Activity activity) {
    DisplayMetrics displayMetrics = ((Context) activity).getResources().getDisplayMetrics();

    float screenHeightInDP = displayMetrics.heightPixels / displayMetrics.density;

    return Math.round(screenHeightInDP);
}

Respondido el 20 de junio de 20 a las 10:06

I had the same need to be able to display my own ads when AdMob fails to receive an ad or receives an empty ad (height=0).

I use the following code based on the fact that an AdView extends RelativeLayout:

mAdMobView = new AdView(pActivity, AdSize.SMART_BANNER, Constants.ADMOB_AD_UNIT_ID);
mAdMobView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(final View pV, final int pLeft, final int pTop, final int pRight, final int pBottom, final int pOldLeft, final int pOldTop, final int pOldRight, final int pOldBottom) {
        final float lAdHeight = mAdMobView.getHeight();
        if (lAdHeight == 0) {
            Debug.i(LOG_TAG, "mAdMobView.onLayoutChange(...) mAdMobView.height='" + lAdHeight + "'. AdMob returned an empty ad !");
            // Show custom ads
        } else {
            Debug.d(LOG_TAG, "mAdMobView.onLayoutChange(...) mAdMobView.height='" + lAdHeight + "'");
            // Make AdView visible
        }
    }
});
mAdMobView.setAdListener(new AdListener() {
    @Override public void onReceiveAd(final Ad pAd) {
        Debug.d(LOG_TAG, "onReceiveAd(...) AdMob ad received (mAdMobView.visibility='" + mAdMobView.getVisibility() + "').");
    }
    @Override public void onPresentScreen(final Ad pAd) {
        Debug.d(LOG_TAG, "onPresentScreen(...)");
    }
    @Override public void onLeaveApplication(final Ad pAd) {
        Debug.d(LOG_TAG, "onLeaveApplication(...)");
    }
    @Override public void onDismissScreen(final Ad pAd) {
        Debug.d(LOG_TAG, "onDismissScreen(...)");
    }

    @Override
    public void onFailedToReceiveAd(final Ad pAd, final ErrorCode pErrorCode) {
        Debug.i(LOG_TAG, "onFailedToReceiveAd(...) AdMob ad error (" + pErrorCode + ").");
        // Show custom ads
    }
});

The code in 'onLayoutChange' is executed every time Admob receives a new ad.

EDIT: My answer is not proper since this method was added with the API 11... I changed it for the use of onPreDraw() as explained in the previous answer.

Respondido 16 ago 12, 16:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.