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:
===
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?
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.
Banner ad guidance: 300x250 - Google AdMob Help.
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.
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);
...
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With