I have an app written fully with compose,i working...
# compose
m
I have an app written fully with compose,i working about how to implement native ads in compose but i cant.Someone can help me?
a
the
AndroidView
composable is likely what you want
m
I've been dealing for 5-6 hours but I couldn't solve it.I cant inflate template view in Android View widget.How could there not be a better example of this?
a
what have you tried so far?
m
I imported the project in this link. Then I was able to get the NativeAd object for populate. But I cannot inflate the template view. Nothing appears on the screen.
Code snippet about problem
Copy code
private fun populateTemplateView(nativeAd: NativeAd) = TemplateView(context).apply {
    val cd = ColorDrawable(resources.getColor(R.color.white))
    val styles =
        NativeTemplateStyle.Builder().withMainBackgroundColor(cd).build()
    val layout  = View.inflate(context,R.layout.native_ad_layout,null)
    val template: TemplateView = layout.findViewById(R.id.my_template)
    template.setStyles(styles)
    template.setNativeAd(nativeAd)
}
p
And what it is the error when you compile?
m
I have create this gist to provide template view.https://gist.github.com/mehmetpeker/fbc43f8135a7cab42a58e687b40268ec.
My error PID: 18275 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:5247) at android.view.ViewGroup.addView(ViewGroup.java:5076) at android.view.ViewGroup.addView(ViewGroup.java:5016) at android.view.ViewGroup.addView(ViewGroup.java:4988) at androidx.compose.ui.viewinterop.AndroidViewHolder.setView$ui_release(AndroidViewHolder.android.kt:83) at androidx.compose.ui.viewinterop.ViewFactoryHolder.setFactory(AndroidView.android.kt:157) at androidx.compose.ui.viewinterop.AndroidView_androidKt$AndroidView$1.invoke(AndroidView.android.kt:99) at androidx.compose.ui.viewinterop.AndroidView_androidKt$AndroidView$1.invoke(AndroidView.android.kt:96) at androidx.compose.ui.viewinterop.AndroidView_androidKt$AndroidView$$inlined$ComposeNode$1.invoke(Composables.kt:225) at androidx.compose.runtime.ComposerImpl$createNode$2.invoke(Composer.kt:1365) at androidx.compose.runtime.ComposerImpl$createNode$2.invoke(Composer.kt:1363) at androidx.compose.runtime.ComposerImpl$recordInsert$2.invoke(Composer.kt:2768) at androidx.compose.runtime.ComposerImpl$recordInsert$2.invoke(Composer.kt:2765) at androidx.compose.runtime.CompositionImpl.applyChanges(Composition.kt:637) at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:488) at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:425) at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:34) at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109) at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41) at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1035) at android.view.Choreographer.doCallbacks(Choreographer.java:845) at android.view.Choreographer.doFrame(Choreographer.java:775) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1022) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7839) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) I/Process: Sending signal. PID: 18275 SIG: 9
a
that code is doing a lot more churning of views than you probably want or need, see https://developer.android.com/jetpack/compose/interop/interop-apis#views-in-compose
m
Copy code
if(nativeAdLoaded){
    AndroidView(factory = {
     
        naviteAdProvider.getTemplateView()

    })
}
I think problem is not related with this.I think my problem on populate view function.I dont know maybe i cant inflate template view.I need to help with this
I am very surprised and angry how there is no official support in jetpack compose for a monetization, which is one of the biggest goals of developers.
a
I am sorry that you are frustrated. The
NativeAdProvider
class as posted is returning
template
instead of
layout
.
template
is attached to a parent view already, as the exception message describes; it's a sub-view of the inflated layout. You likely want to return the whole inflated layout root. The caching of that view that this class is performing may also lead to further issues of this sort.
m
Copy code
Thank you very much for all your help and effort. Thanks to you, I solved a problem, but I couldn't succeed again. I found that the problem was in a very different place. As soon as I changed the template type, my problem was fixed. I changed the template type from small to medium and my problem was solved.
Copy code
<com.google.android.ads.nativetemplates.TemplateView
android:id="@+id/my_template"
app:gnt_template_type="@layout/gnt_small_template_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
to
Copy code
<com.google.android.ads.nativetemplates.TemplateView
android:id="@+id/my_template"
app:gnt_template_type="@layout/gnt_medium_template_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
then problem fixed.
👍 1