We are trying to display admob ads in listview. We may show ads in every 10 objects so we are trying to preload 3 ads at the time of onCreate(). Following is the code snippet for loading ads.
AdRequest request = new AdRequest.Builder()
.addTestDevice("C6736622852CDF40B5FC25A9DF38DAAB")
.build();
final NativeExpressAdView adView = (NativeExpressAdView)View.inflate(context, R.layout.admob_ad_view, null);
adView.loadAd(request);
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
listOfAds.add(adView);
}
}
With above code, I am able to create 3 adviews and store in a listview, while user scrolls down the listview, we are loading ads from this list. We create a empty linear layout for recyclerview element and in onBind(), we are adding this adView object to that linear layout.
linearlayout.addView(adView);
ISSUE: It works fine for 75% of the time. Other times, it shows an empty cell. When user tries to scroll or tap on the empty cell, adview gets refreshed and displays the ad perfectly.
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="@string/SHORT_ADMOB_AD_UNIT_ID"
ads:adSize="FULL_WIDTHx80">
</com.google.android.gms.ads.NativeExpressAdView>
compile('com.google.firebase:firebase-ads:9.2.0') { force = true; }
What would be the issue?