Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Admob on Android: banner space not reserved before loading

We have been using AdMob on our Android app for more than 4 years. In the last days, we encountered an issue with AdMob, without modifying any code.

As you can see from the picture below:

  • PREVIOUSLY, the banner space was reserved, before the banner was loaded
  • NOW, the banner space is not reserved before loading, creating a very annoying experience for the user, who sees content shifting down after the banner is loaded

enter image description here

===

Here is a description of our implementation:

we are placing our banner about 20% top of the screen of a fragment, inside a LinearLayout "banner_container"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
....
<LinearLayout android:id="@+id/banner_container"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
....
</LinearLayout>

on Fragment's "onCreateView" we are adding the banner to the container

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

...

adView = new AdView(getActivity()); 
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);

LinearLayout mBannerContainer = rootView.findViewById(R.id.banner_container); 
mBannerContainer.setVisibility(View.VISIBLE); 
mBannerContainer.addView(adView);

AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build();

adView.loadAd(adRequest);

...

}

===

How can we revert to the situation where the banner space is already reserved on loading?

like image 910
Daniele B Avatar asked Nov 06 '18 05:11

Daniele B


People also ask

Why my AdMob ads are not showing in app?

Ads won't show if you haven't integrated the Google Mobile Ads SDK correctly. Is your ad implementation code working properly? Test your implementation code to check that ads can show. You can also use ad inspector to test your app's ad serving.

What is size of AdMob banner?

Banner ad guidance: 300x250 - Google AdMob Help.

Where do banner ads appear?

Banner ads are placed in high-traffic locations on web pages, creating brand awareness and generating click-throughs, purchases, and leads. These high-visibility locations include the front, bottom, or the side of a webpage; places where the eyes of browsers usually wander.


2 Answers

Since Admob does not reserve the banner height before loading it, the solution seems to be to do it manually.

According to the Admob guide for Banner Ads:

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

Solution 1

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 >= 400 && 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);
     }

In your "banner_container" remove:

android:visibility="gone"

Change Fragment "onCreateView":

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

         ...

         adView = new AdView(getActivity()); 
         adView.setAdSize(AdSize.SMART_BANNER);
         adView.setAdUnitId(AD_UNIT_ID);

         LinearLayout mBannerContainer = rootView.findViewById(R.id.banner_container); 
         mBannerContainer.setLayoutParams(
              new LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            getAdViewHeightInDP(this.getActivity())
               ));
         )
         mBannerContainer.addView(adView);

         AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build();

         adView.loadAd(adRequest);

         ...

     }

Solution 2

Instead of using the methods "getAdViewHeightInDP" and "getScreenHeightInDP", the method "AdSize.SMART_BANNER.getHeightInPixels (this)" is used.

In your "banner_container" remove:

android:visibility="gone"

Change Fragment "onCreateView":

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

         ...

         adView = new AdView(getActivity()); 
         adView.setAdSize(AdSize.SMART_BANNER);
         adView.setAdUnitId(AD_UNIT_ID);

         LinearLayout mBannerContainer = rootView.findViewById(R.id.banner_container); 
         mBannerContainer.setLayoutParams(
              new LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            AdSize.SMART_BANNER.getHeightInPixels(this.getActivity())
               ));
         )
         mBannerContainer.addView(adView);

         AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build();

         adView.loadAd(adRequest);

         ...

     }
like image 75
Winner Sanchez Avatar answered Oct 09 '22 23:10

Winner Sanchez


Just to reiterate from the comment and clarify with codeblock, it was easiest to do this:

LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout); layout.addView(adView, 0); adView.getLayoutParams().height = AdSize.SMART_BANNER.getHeightInPixels(this);

I really wish Admob informed of this change in behaviour to ad loading so that developers were aware and can avoid accidental clicks.

like image 4
Jsyntax Avatar answered Oct 09 '22 23:10

Jsyntax