androidkotlinadmobnative-adsgoogle-ads-api

How to preload native ads, android?


I added a single Native ad to my application, but on different devices, ads, especially videos, come with a delay, on the emulator the video is not shown at all. I know that you can somehow preload ads, but I don’t know how to do it, please explain to me.

Here is my code:

class Fragment(private var index: Int, private var adsId: String) : Fragment() {

    var currentNativeAd: UnifiedNativeAd? = null
    lateinit var adLoader: AdLoader

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment, container, false)
        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val unifiedNativeAdView = layoutInflater.inflate(R.layout.native_ad_layout, null) as UnifiedNativeAdView

        val nativeAdLayout = view!!.findViewById<FrameLayout>(R.id.id_native_ad)

        val adLoader = AdLoader.Builder(activity, adsId).forUnifiedNativeAd { unifiedNativeAd ->
(unifiedNativeAd)
            mapUnifiedNativeAdToLayout(unifiedNativeAd, unifiedNativeAdView)
            nativeAdLayout.removeAllViews()

        }.withAdListener(object : AdListener() {
            // Code to be executed when an ad request fails.
            override fun onAdLoaded() {
                super.onAdLoaded()
                if (!adLoader.isLoading) {
                  nativeAdLayout.addView(unifiedNativeAdView)
                }
            }

            override fun onAdFailedToLoad(errorCode: Int) {
               Toast.makeText(applicationContext, "Ad failed to load! error code: $errorCode", Toast.LENGTH_SHORT).show()
            }

            override fun onAdClosed() {
                nativeAdLayout.removeAllViews()

            }

        }).build()
        adLoader.loadAd(AdRequest.Builder().build())
   
    }

    fun mapUnifiedNativeAdToLayout(adFromGoogle: UnifiedNativeAd, myAdView: UnifiedNativeAdView) {
        currentNativeAd?.destroy()
        currentNativeAd = adFromGoogle

        val mediaView: MediaView = myAdView.findViewById(R.id.ad_media)
        myAdView.mediaView = mediaView
        myAdView.bodyView = myAdView.findViewById(R.id.ad_body)

        if (adFromGoogle.body == null) {
            myAdView.bodyView.visibility = View.GONE
        } else {
            (myAdView.bodyView as TextView).text = adFromGoogle.body
        }

        myAdView.setNativeAd(adFromGoogle)

        val vc = adFromGoogle.videoController

        if (vc.hasVideoContent()) {

            vc.videoLifecycleCallbacks = object : VideoController.VideoLifecycleCallbacks() {
                override fun onVideoEnd() {
                    super.onVideoEnd()
                }
            }
        } 
    }

    override fun onDestroy() {
        currentNativeAd?.destroy()
        super.onDestroy()

    }
}

native_ad_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.formats.UnifiedNativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="2"
                android:orientation="vertical">

                <com.google.android.gms.ads.formats.MediaView
                    android:id="@+id/ad_media"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:padding="@dimen/_8sdp"
                    android:layout_gravity="center"
                    android:layout_weight="2"
                    android:background="#fff" />

                <TextView
                    android:id="@+id/ad_body"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/sf_pro_display_regular"
                    android:gravity="center"
                    android:text="body of ad"
                    android:textSize="14sp" />

            </LinearLayout>

        </RelativeLayout>
    
    </LinearLayout>

</com.google.android.gms.ads.formats.UnifiedNativeAdView>

In a fragment.xml, just a FrameView for the ad.


Solution

  • using java there was a method that's on the google docs that mentions using adloader.isLoading

    so in your

    override fun onAdLoaded() {
                    super.onAdLoaded()
    
                }
    
    you would ad if(!adLoader.isLoading){
        //insert ads to your recycler.
    }
    

    so..

        override fun onAdLoaded() {
                        super.onAdLoaded()
                       if(!adLoader.isLoading){
    
        int number_of_posts_before_showing_ad = 5
    
      if (nativeAds.size() <= 0 || yourPostList.size() < number_of_posts_before_showing_ad) {
                return;
            } else {
                int offset = (yourPostList.size() / nativeAds.size() + 1);
                int index = 0;
                for (UnifiedNativeAd ad : nativeAds) {
                    yourPostList.add(index, ad);
                    recyclerViewAdapter.notifyDataSetChanged();
                    index += offset;
                }
            }
        }
        
                    }