Sometimes you want to use new Compose UI together with old Android Views. This helps you add new features without rewriting everything.
Compose with existing Views (interop) in Android Kotlin
/* To use Compose inside Views (XML or ViewGroup): */ val composeView = ComposeView(context).apply { setContent { // Compose UI here } } /* To use Views inside Compose: */ AndroidView(factory = { context -> TextView(context).apply { text = "Hello from View" } })
ComposeView lets you put Compose UI inside traditional Views.
AndroidView lets you put Android Views inside Compose UI.
val composeView = ComposeView(context).apply {
setContent {
Text("Hello Compose inside View")
}
}AndroidView(factory = { context ->
Button(context).apply {
text = "Click me"
}
})AndroidView(factory = { context ->
EditText(context).apply {
hint = "Enter text"
}
})This app screen shows a Compose Text and below it an Android View TextView inside Compose using AndroidView.
import android.os.Bundle import android.widget.TextView import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Column import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.viewinterop.AndroidView class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { Column { Text(text = "This is Compose Text") AndroidView(factory = { context -> TextView(context).apply { text = "This is Android View TextView" textSize = 20f } }) } } } }
Remember to manage lifecycle properly when mixing Views and Compose.
AndroidView can be used to wrap any existing View, including custom Views.
ComposeView can be added to XML layouts or ViewGroups to show Compose UI.
You can mix Compose and Views using ComposeView and AndroidView.
This helps migrate apps gradually or reuse existing UI parts.
Use ComposeView to put Compose inside Views, and AndroidView to put Views inside Compose.
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
