Jetpack Compose is a modern, declarative toolkit for building native Android UIs. It simplifies UI development on Android with powerful tools and intuitive Kotlin APIs.
Spatial SDK supports Jetpack Compose and other UI frameworks. Use Jetpack Compose within Spatial SDK to build 2D panels. Preview and edit them in real time using the Live Edit feature.
There are two ways to create panels in a 3D scene with Jetpack Compose:
View-based: Lightweight and performant
Activity-based: More flexible and powerful, comes with a performance cost
Create Android views directly with Jetpack Compose
Use Jetpack Compose directly within an Android view for better performance. This approach eliminates additional activities, conserves resources, and enhances overall performance.
Gradle dependency
Add the meta-spatial-sdk-compose library to your Gradle dependencies. This library contains the ComposeFeature class with helper functions that simplify using Jetpack Compose in Spatial SDK.
// Add ComposeFeature in the immersive activity
override fun registerFeatures(): List<SpatialFeature> {
return listOf(..., ComposeFeature())
}
Register a view-based panel with Jetpack Compose
The meta-spatial-sdk-compose library includes a special PanelRegistration class called ComposeViewPanelRegistration.
Create standalone activities that use Jetpack Compose for more complex UIs that need full activity lifecycle management. This approach provides greater flexibility and access to Android’s activity features. Use this when you need features like activity-specific navigation, complex state management, or integration with other activity-based libraries.
Define the Android Activity that displays Jetpack Compose UI
First, define your Jetpack Compose UI.
@Composable
fun MessageCard(name: String) {
Text(text = "Hello $name!")
}
Create the activity for the panel and set the content to the Jetpack Compose UI.
Define the activity in your AndroidManifest.xml and include the tag android:allowEmbedded="true". This allows the activity to be embedded into a panel.
Register and spawn the panel
After creating the activity with Jetpack Compose, register the panel using PanelRegistration:
Run UI code on a separate thread by placing your Activity in a separate process. See Android processes and threads for details. This approach requires inter-process communication (IPC) between activities, so use it carefully.