What if you could add shiny new UI parts without breaking your whole app?
Why Compose with existing Views (interop) in Android Kotlin? - Purpose & Use Cases
Imagine you have an Android app with many screens built using old Views like Buttons, TextViews, and RecyclerViews. Now you want to add new, modern UI parts using Jetpack Compose without rewriting everything.
Rebuilding the whole app from scratch is slow and risky. Mixing old Views and new Compose UI manually means writing complex code to manage both, which is error-prone and hard to maintain.
Compose with existing Views interop lets you smoothly combine Compose UI and traditional Views in the same screen. You can reuse old Views inside Compose or add Compose UI inside View layouts easily.
val oldButton = findViewById<Button>(R.id.old_button)
oldButton.setOnClickListener { /* handle click */ }AndroidView(factory = { context -> Button(context).apply { setOnClickListener { /* handle click */ } } })You can modernize your app step-by-step, mixing new Compose UI with existing Views without rewriting everything.
A shopping app wants to add a fancy Compose-based product card but keep the old cart screen built with Views. Interop lets both UI styles work together seamlessly.
Old Views and new Compose UI can live together smoothly.
No need to rewrite entire app to use Compose.
Interop saves time and reduces bugs when mixing UI types.
Practice
AndroidView in Jetpack Compose?Solution
Step 1: Understand AndroidView role
AndroidViewallows embedding existing Android Views inside Compose UI, enabling reuse of legacy UI parts.Step 2: Compare options
Creating animations, converting Compose UI elements into XML layouts, and automatically replacing all Views with Compose components do not describe embedding Views inside Compose. Only embedding traditional Android Views inside a Compose UI correctly states the purpose.Final Answer:
To embed traditional Android Views inside a Compose UI -> Option DQuick Check:
AndroidView = Embed Views in Compose [OK]
- Confusing AndroidView with ComposeView
- Thinking AndroidView converts Compose to XML
- Assuming AndroidView replaces Views automatically
Solution
Step 1: Identify ComposeView usage
ComposeViewis a View that hosts Compose UI inside a traditional View hierarchy.Step 2: Eliminate wrong options
UseAndroidViewinside the View XML layout is wrong because AndroidView is for Compose to View embedding, not View XML. Replace the View with a Compose function directly is invalid as Views cannot be replaced directly by Compose functions. UsesetContentViewwith Compose functions is for setting Compose as the whole content, not embedding.Final Answer:
Use ComposeView inside the View hierarchy -> Option BQuick Check:
ComposeView = Compose inside Views [OK]
- Using AndroidView to embed Compose in Views
- Trying to replace Views with Compose functions directly
- Confusing setContentView with embedding Compose
AndroidView(factory = { context ->
Button(context).apply {
text = "Click Me"
}
})Solution
Step 1: Analyze AndroidView factory
The factory creates a traditional Android Button with text "Click Me" inside Compose UI.Step 2: Understand output
AndroidView embeds the View, so the Button is shown as a View inside Compose, not a Compose Button.Final Answer:
A traditional Android Button with text "Click Me" inside Compose -> Option CQuick Check:
AndroidView shows Views inside Compose [OK]
- Thinking AndroidView creates Compose components
- Expecting runtime error for Button usage
- Confusing Android Button with Compose Button
AndroidView(factory = { context ->
TextView(context).apply {
setText("Hello")
}
}) {
text = "World"
}Solution
Step 1: Check AndroidView usage
The trailing lambda is for updating the View, buttextis not a Compose property; it should be set on the View inside the factory or update block.Step 2: Identify correct usage
Settingtextoutside the factory like this causes error becausetextis not recognized in that scope.Final Answer:
Cannot set text inside the trailing lambda; it should be inside factory -> Option AQuick Check:
Set View properties inside factory or update lambda [OK]
- Trying to set View properties directly in trailing lambda
- Assuming trailing lambda is Compose scope
- Ignoring factory return value requirements
Solution
Step 1: Understand gradual migration
Gradual migration means mixing Compose and Views without rewriting everything at once.Step 2: Identify correct interop usage
ComposeView embeds Compose inside Views; AndroidView embeds Views inside Compose. Using both allows smooth transition.Step 3: Eliminate wrong options
Rewriting all Views to Compose before running the app, using only AndroidView everywhere to avoid Compose, and replacing setContentView with Compose functions and removing all Views immediately are extreme or incorrect approaches that do not support gradual migration.Final Answer:
Use ComposeView to add Compose UI inside existing View layouts and AndroidView to add Views inside Compose screens -> Option AQuick Check:
ComposeView + AndroidView = smooth migration [OK]
- Trying to rewrite entire app at once
- Using only AndroidView or only ComposeView
- Removing Views before Compose is ready
Ready to prove it?
Pass the test and this pattern moves unknown
