Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android get adMob banner height when appears

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();
    }
like image 975
Jaume Avatar asked May 03 '12 14:05

Jaume


People also ask

What is smart banner AdMob?

Smart Banners are ad units that render screen-width banner ads on any screen size across different devices in either orientation. Smart Banners detect the width of the device in its current orientation and create the ad view that size. Three ad heights are implemented in smart banners: Ad height.

What are adaptive ads?

Adaptive banners are the newest generation of banner ads and offer the best user experience. They're intended to replace smart and standard banners. Note: Support for smart banners is deprecated. Consider implementing adaptive banners as a replacement for smart banners. Type.

How do I use AdMob banner ads?

Click Create ad unit. Follow the Google Developers instructions for Android, iOS, or Unity to implement banner ad units in your app code. You will need your app ID and ad unit ID during implementation. This ad unit won't show ads until you've completed this step.


1 Answers

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();
}

Just change AdSize.BANNER to AdSize.SMART_BANNER or whatever banner type your using.

Add Sizes Get Height

like image 56
XdebugX Avatar answered Oct 18 '22 07:10

XdebugX