Admob Native Ads inside a Recyclerview displays blank space before loading
You can have your native_ad.xml
just contain an empty LinearLayout
and add the NativeExpressAdView
dynamically to the layout once the ad is loaded.
native_ad.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Create an object of NativeExpressAdView dynamically and add it to the layout only once the ad is loaded.
public class NativeAdViewHolder extends RecyclerView.ViewHolder {
private final LinearLayouot containerView;
public NativeAdViewHolder(final View itemView) {
super(itemView);
containerView = itemView;
NativeExpressAdView mNativeAd = new NativeExpressAdView(itemView.getContext());
mNativeAd.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
mNativeAd.setId(R.id.nativeAd);
mNativeAd.setAdSize(new AdSize(AdSize.FULL_WIDTH, 132));
mNativeAd.setAdUnitId("yourAdUnitId");
mNativeAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
containerView.addView(mNativeAd);
}
...All other overriden methods
});
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mNativeAd.loadAd(adRequest);
}
}
you may set visibility of view untill it loaded if you are not adding dynamically
public NativeAdViewHolder(final View itemView) {
super(itemView);
itemView.setVisibility(View.GONE);
mNativeAd = (NativeExpressAdView) itemView.findViewById(R.id.nativeAd);
mNativeAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
itemView.setVisibility(View.VISIBLE);
}
@Override
public void onAdClosed() {
super.onAdClosed();
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
}
@Override
public void onAdLeftApplication() {
super.onAdLeftApplication();
}
@Override
public void onAdOpened() {
super.onAdOpened();
}
});
similarly do in onadfailedtoload as requirement i hope it may help you.