Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – can’t show admob native advanced ad inside RecyclerView

Posted on November 11, 2022 By

Solution 1 :

Please check your method:

getItemViewType(position: Int): Int 

It might not returning the correct itemView type

Problem :

I want to show an advanced native ad inside a RecyclerView

this is the code I have tried inside the adabter

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ITEM_SONG -> {
                val view = LayoutInflater.from(parent.context)
                        .inflate(R.layout.song_card, parent, false)
                SongViewHolder(view)
            }
            else -> {
                val bannerView = LayoutInflater.from(parent.context)
                        .inflate(R.layout.ad_frame, parent)
                AdViewHolder(bannerView)
            }
        }
    }

    private fun populateUnifiedNativeAdView(nativeAd: UnifiedNativeAd, adView: UnifiedNativeAdView) {
        adView.mediaView = adView.findViewById(R.id.ad_media)

        adView.headlineView = adView.findViewById(R.id.ad_headline)
        adView.bodyView = adView.findViewById(R.id.ad_body)
        adView.callToActionView = adView.findViewById(R.id.ad_call_to_action)

        (adView.headlineView as TextView).text = nativeAd.headline
        adView.mediaView.setMediaContent(nativeAd.mediaContent)

        if (adView.bodyView == null) {
            adView.bodyView.visibility = View.INVISIBLE
        } else {
            adView.bodyView.visibility = View.VISIBLE
            (adView.bodyView as TextView).text = nativeAd.body
        }

        if (adView.callToActionView == null) {
            adView.callToActionView.visibility = View.INVISIBLE
        } else {
            adView.callToActionView.visibility = View.VISIBLE
            (adView.callToActionView as TextView).text = nativeAd.callToAction
        }
        adView.setNativeAd(nativeAd)
    }

inside the onBindViewHolder

ITEM_BANNER -> {
                val adViewHolder = holder as AdViewHolder
                val parent = adViewHolder.itemView as ViewGroup
                val adView = LayoutInflater.from(App.context)
                        .inflate(R.layout.ad_unified, null) as UnifiedNativeAdView
                val builder = AdLoader.Builder(App.context, NATIVE_ID)
                        .forUnifiedNativeAd { unifiedNativeAd: UnifiedNativeAd? ->
                            if (unifiedNativeAd != null) {
                                populateUnifiedNativeAdView(unifiedNativeAd, adView)
                                parent.removeAllViews()
                                parent.addView(adView)
                            }
                        }

                val videoOptions = VideoOptions.Builder()
                        .setStartMuted(true)
                        .build()

                val adOptions = NativeAdOptions.Builder()
                        .setVideoOptions(videoOptions)
                        .build()

                builder.withNativeAdOptions(adOptions)

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

in MainActivity this code is used to add the FrameLayout to the list of items

private fun getNativeAds(list: ArrayList<Any>) {
        var i = 0
        while (i < list.size) {
            list.add(i, FrameLayout([email protected]))
            i += ITEMS_PER_AD
        }
    }

I get this error

java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to c.c.a.b.a
        at c.c.a.a.a.b(:82)
        at androidx.recyclerview.widget.RecyclerView$g.c(:7065)
        at androidx.recyclerview.widget.RecyclerView$g.a(:7107)
        at androidx.recyclerview.widget.RecyclerView$v.a(:6012)
        at androidx.recyclerview.widget.RecyclerView$v.a(:6279)
        at androidx.recyclerview.widget.RecyclerView$v.b(:6118)
        at androidx.recyclerview.widget.RecyclerView$v.d(:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$c.a(:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.a(:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.a(:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.c(:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(:4404)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(:1915)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(:446)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:755)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2422)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2142)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1299)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
        at android.view.Choreographer.doCallbacks(Choreographer.java:683)
        at android.view.Choreographer.doFrame(Choreographer.java:619)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6316)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

Comments

Comment posted by Waqar UlHaq

can you please share xml layout?

READ  [FIXED] mongodb - How to manage an offline Mongo Database in Android
Powered by Inline Related Posts

Comment posted by Omar Qataberi

I have edited the question please check it out @WaqarUlHaq

Comment posted by Waqar UlHaq

I would be more interested in layout where you have FrameLayout and RecyclerView

Comment posted by Omar Qataberi

I made a

Comment posted by Waqar UlHaq

getItemViewType(position: Int): Int will tell you the type of

Android Tags:admob, android, kotlin, native-ads

Post navigation

Previous Post: [FIXED] android – Convert BMP image to PNG or JPG in Java without loading the entire image in memory
Next Post: [FIXED] xml – Android – prevent TextView to push out another element inside LinearLayout

Related Posts

[FIXED] kotlin – WorkManager in Android to handle producer/consumer pattern for data received in FCM Android
[FIXED] google cloud platform – how to use local sqlite database along with firebase database android Android
[FIXED] Android navigation component resets to start destination after configuration change Android
[FIXED] android – Programmatically add Fragment to an Activity in Kotlin Android
[FIXED] android – How to set a variable before calling default constructor in java? Android
[FIXED] java – NavigationView on a null object reference in Mapbox Android Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme