0

I successfully set Admob native ads in my app, but the video not showing in the media content, it shows an empty area even the mediaContent is not null, I don't know if this problem happens in real code id and the app must be published in the store, but I tried the real unit id

enter image description here

My code

native_ad_row xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/nativeAdView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#FFFFFF"
        android:minHeight="50dp"
        android:orientation="vertical">

        <TextView style="@style/Theme.DummyAppKotlin.AdAttribution" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="20dp"
            android:paddingTop="3dp"
            android:paddingRight="20dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/ad_app_icon"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:adjustViewBounds="true"
                    android:paddingEnd="5dp"
                    android:paddingBottom="5dp" />

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

                    <TextView
                        android:id="@+id/ad_headline"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColor="#0000FF"
                        android:textSize="16sp"
                        android:textStyle="bold" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@+id/ad_advertiser"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:gravity="bottom"
                            android:textSize="14sp"
                            android:textStyle="bold" />

                        <RatingBar
                            android:id="@+id/ad_stars"
                            style="?android:attr/ratingBarStyleSmall"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:isIndicator="true"
                            android:numStars="5"
                            android:stepSize="0.5" />
                    </LinearLayout>

                </LinearLayout>
            </LinearLayout>

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

                <TextView
                    android:id="@+id/ad_body"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="20dp"
                    android:textSize="12sp" />

                <com.google.android.gms.ads.nativead.MediaView
                    android:id="@+id/ad_media"
                    android:layout_width="match_parent"
                    android:layout_height="175dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="5dp" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end"
                    android:orientation="horizontal"
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp">

                    <TextView
                        android:id="@+id/ad_price"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingStart="5dp"
                        android:paddingLeft="5dp"
                        android:paddingEnd="5dp"
                        android:paddingRight="5dp"
                        android:textSize="12sp" />

                    <TextView
                        android:id="@+id/ad_store"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingStart="5dp"
                        android:paddingLeft="5dp"
                        android:paddingEnd="5dp"
                        android:paddingRight="5dp"
                        android:textSize="12sp" />

                    <Button
                        android:id="@+id/ad_call_to_action"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textSize="12sp" />

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>


</com.google.android.gms.ads.nativead.NativeAdView>

The NativeAdViewholder

    inner class AdViewHolder(private val binding: NativeAdRowBinding) :
        RecyclerView.ViewHolder(binding.root) {

        private val videoOptions = VideoOptions.Builder()
            .setStartMuted(false)
            .build()

        fun bindAdData() {
            val adLoader =
                AdLoader.Builder(binding.root.context, "ca-app-pub-3940256099942544/1044960115")
                    .forNativeAd { nativeAd: NativeAd ->

                        this@PostAdapter.nativeAd = nativeAd
                        populateNativeADView(nativeAd)



                    }
                    .withAdListener(object : AdListener() {

                        override fun onAdClicked() {
                            super.onAdClicked()
                            Log.d(TAG, "onAdClicked: ")
                        }

                        override fun onAdClosed() {
                            super.onAdClosed()
                            Log.d(TAG, "onAdClosed: ")
                        }

                        override fun onAdLoaded() {
                            super.onAdLoaded()
                            Log.d(TAG, "onAdLoaded: ")
                        }

                        override fun onAdOpened() {
                            super.onAdOpened()
                            Log.d(TAG, "onAdOpened: ")
                        }

                        override fun onAdFailedToLoad(adError: LoadAdError) {
                            // Handle the failure by logging, altering the UI, and so on.
                            Toast.makeText(
                                binding.root.context,
                                adError.message,
                                Toast.LENGTH_SHORT
                            ).show()

                            Log.e(TAG, "onAdFailedToLoad: ${adError.cause.toString()}")
                        }
                    })
                    .withNativeAdOptions(
                        NativeAdOptions.Builder()
                            // Methods in the NativeAdOptions.Builder class can be
                            // used here to specify individual options settings.
                            .setVideoOptions(videoOptions).build()
                    )
                    .build()

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

        private fun populateNativeADView(nativeAd: NativeAd) {
            val headLine = nativeAd.headline
            val body = nativeAd.body
            val callToAction = nativeAd.callToAction
            val icon = nativeAd.icon
            val price = nativeAd.price
            val store = nativeAd.store
            val storeRating = nativeAd.starRating
            val advertiser = nativeAd.advertiser
            val mediaContent = nativeAd.mediaContent

            if (headLine.isNullOrEmpty()) {
                binding.adHeadline.visibility = INVISIBLE
            } else {
                binding.adHeadline.visibility = View.VISIBLE
                binding.adHeadline.text = headLine
            }

            if (body.isNullOrEmpty()) {
                binding.adBody.visibility = INVISIBLE
            } else {
                binding.adBody.visibility = View.VISIBLE
                binding.adBody.text = headLine
            }
            if (icon == null) {
                binding.adAppIcon.visibility = INVISIBLE
            } else {
                binding.adAppIcon.visibility = View.VISIBLE
                binding.adAppIcon.setImageDrawable(icon.drawable)
            }
            if (storeRating == null) {
                binding.adStars.visibility = INVISIBLE
            } else {
                binding.adStars.visibility = View.VISIBLE
                binding.adStars.rating = storeRating.toFloat()
            }
            if (price.isNullOrEmpty()) {
                binding.adPrice.visibility = INVISIBLE
            } else {
                binding.adPrice.visibility = View.VISIBLE
                binding.adPrice.text = price
            }
            if (store.isNullOrEmpty()) {
                binding.adStore.visibility = INVISIBLE
            } else {
                binding.adStore.visibility = View.VISIBLE
                binding.adStore.text = store
            }
            if (advertiser.isNullOrEmpty()) {
                binding.adAdvertiser.visibility = INVISIBLE
            } else {
                binding.adAdvertiser.visibility = View.VISIBLE
                binding.adAdvertiser.text = advertiser
            }
            if (mediaContent == null ) {
                binding.adMedia.visibility = INVISIBLE
            } else {
                binding.adMedia.visibility = View.VISIBLE
                binding.adMedia.setMediaContent(mediaContent)
                Log.d(TAG, "setMediaContent: ${mediaContent.duration.toString()}")
            }
            if (callToAction.isNullOrEmpty()) {
                binding.adCallToAction.visibility = INVISIBLE
            } else {
                binding.adCallToAction.visibility = View.VISIBLE
                binding.adCallToAction.text = callToAction
                binding.nativeAdView.callToActionView = binding.adCallToAction

            }

            binding.nativeAdView.setNativeAd(nativeAd)
        }

in mainActivity

 val testDeviceIds = Arrays.asList("ASOMEIDS")
        val configuration = RequestConfiguration.Builder().setTestDeviceIds(testDeviceIds).build()
        MobileAds.setRequestConfiguration(configuration)
        if (Utils.hasInternetConnection(this)) {
            MobileAds.initialize(this) {
                Log.d(TAG, "onViewCreated: onInitCompleted")
            }
        }
Dr Mido
  • 2,414
  • 4
  • 32
  • 72

1 Answers1

0

If you need to test it with a test key you need to replace the val testDeviceIds = Arrays.asList("ASOMEIDS") with your test device id, you can get it from your log once you run your app as this I/Ads: Use RequestConfiguration.Builder.setTestDeviceIds(Arrays.asList("33BE2250B43518CCDA7DE426D04EE231")) to get test ads on this device."

or simply you can test it on an emulator because Android emulators are automatically configured as test devices.

https://developers.google.com/admob/android/test-ads check this out.