1

I have implemented Native Ads in my app. Everything works fine, except for one problem:

If the app has a star rating, it always returns 0.0.

Here's my code:

private fun loadAd() {
    val builder = AdLoader.Builder(context, getString(R.string.test_admob_app_unit_id))
    /** Build ad */
    builder
        .forUnifiedNativeAd { unifiedNativeAd ->
            /** Populate UnifiedNativeAdView with loaded NativeAd */
            if (activity == null) return@forUnifiedNativeAd
            val unifiedNativeAdView = layoutInflater.inflate(R.layout.ad_unified, null) as UnifiedNativeAdView
            populateUnifiedNativeAdView(unifiedNativeAd, unifiedNativeAdView)


private fun populateUnifiedNativeAdView(nativeAd: UnifiedNativeAd, adView: UnifiedNativeAdView) {
    currentNativeAd?.destroy()
    currentNativeAd = nativeAd
    /** Set the media view */
    adView.mediaView = adView.findViewById<MediaView>(R.id.ad_media)
    adView.mediaView.setImageScaleType(ImageView.ScaleType.FIT_START)
    adView.mediaView.setMediaContent(nativeAd.mediaContent)
    adView.headlineView = adView.findViewById(R.id.headlineText)
    adView.bodyView = adView.findViewById(R.id.bodyText)
    adView.callToActionView = adView.findViewById(R.id.meet)
    adView.iconView = adView.findViewById(R.id.adIcon)
    /** Rating */
    val starRatingView = adView.findViewById<RatingBar>(R.id.rating)
    log("starRating: ${nativeAd.starRating}") // prints 0.0
    log("store: ${nativeAd.store}") // prints Google Play
    nativeAd.starRating.let {
        /** If rating is not null and greater than 0, show rating */
        if (it != null && it > 0.0) starRatingView.rating = it.toFloat() else starRatingView.visibility = View.GONE
    }
    (adView.headlineView as TextView).text = nativeAd.headline
   ...

If the ad is not an app, the rating will be null. If it is an app, the rating will be 0.0.

These are test ads, does that affect the rating?

Zorgan
  • 8,227
  • 23
  • 106
  • 207

0 Answers0