How can i use ```MediaRouteButton``` in composabl...
# compose
i
How can i use
Copy code
MediaRouteButton
in composables i want have the cast icon not in the actionbar but in my composable?
a
Use the
AndroidView
composable to host it
i
what ist wrong here
Copy code
AndroidView(factory = { context ->
    //Here you can construct your View
    MediaRouteButton(context).apply {
        layoutParams = ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
    }
}, modifier = Modifier
    .padding(8.dp)
    .background(MaterialTheme.colors.onBackground)
)
i getting the follow render problem background can not be translucent: #0
@KoskiA have u some suggestion for it? i use a theme with NoActionBar? but what is the step to working without it? must i set the Color manually? if yes how?
a
Do you have a stack trace for the error? Does the error occur on device, on an emulator, on the android studio preview?
i
yes , i got it on both on device and android studio preview
Copy code
java.lang.IllegalArgumentException: background can not be translucent: #0
	at androidx.core.graphics.ColorUtils.calculateContrast(ColorUtils.java:161)
	at androidx.mediarouter.app.MediaRouterThemeHelper.getControllerColor(MediaRouterThemeHelper.java:164)
	at androidx.mediarouter.app.MediaRouterThemeHelper.getRouterThemeId(MediaRouterThemeHelper.java:257)
	at androidx.mediarouter.app.MediaRouterThemeHelper.createThemedButtonContext(MediaRouterThemeHelper.java:94)
	at androidx.mediarouter.app.MediaRouteButton.<init>(MediaRouteButton.java:125)
	at androidx.mediarouter.app.MediaRouteButton.<init>(MediaRouteButton.java:121)
	at androidx.mediarouter.app.MediaRouteButton.<init>(MediaRouteButton.java:117)
	at com.example.bmradio.ui.components.ComponentsKt$CastButton$1.invoke(Components.kt:218)
	at com.example.bmradio.ui.components.ComponentsKt$CastButton$1.invoke(Components.kt:216)
	at androidx.compose.ui.viewinterop.ViewFactoryHolder.setFactory(AndroidView.android.kt:115)
	at androidx.compose.ui.viewinterop.AndroidView_androidKt$AndroidView$1.invoke(AndroidView.android.kt:81)
	at androidx.compose.ui.viewinterop.AndroidView_androidKt$AndroidView$1.invoke(AndroidView.android.kt:79)
	at androidx.compose.ui.viewinterop.AndroidView_androidKt$AndroidView$$inlined$ComposeNode$1.invoke(Composables.kt:198)
	at androidx.compose.runtime.ComposerImpl$createNode$2.invoke(Composer.kt:1357)
	at androidx.compose.runtime.ComposerImpl$createNode$2.invoke(Composer.kt:1355)
	at androidx.compose.runtime.ComposerImpl$recordInsert$2.invoke(Composer.kt:2697)
	at androidx.compose.runtime.ComposerImpl$recordInsert$2.invoke(Composer.kt:2694)
	at androidx.compose.runtime.CompositionImpl.applyChanges(Composition.kt:562)
	at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:708)
	at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:409)
	at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:144)
	at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:135)
	at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.android.kt:689)
	at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.android.kt:135)
	at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.android.kt:187)
	at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
	at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:196)
	at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:142)
	at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:135)
	at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:753)
	at android.view.View.dispatchAttachedToWindow(View.java:20479)
	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3489)
	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
	at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:44)
	at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:360)
	at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431)
	at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
	at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:714)
	at com.android.tools.idea.rendering.RenderTask.lambda$inflate$7(RenderTask.java:870)
	at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$2.run(RenderExecutor.kt:187)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
a
what theme is specified in your activity that hosts this button?
i
Theme.Material.DayNight .NoActionBar
Parent android:style/Theme.Material.Light.NoActionBar
a
I suspect the MediaRouteButton has other theme requirements, potentially from appcompat. You can construct a
ContextThemeWrapper
around the context given to the
AndroidView
factory block, with a compatible theme that has what
MediaRouteButton
is looking for
1