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
To style the UI inside either panel type, use the prebuilt components in the Meta Horizon OS UI Set overview rather than rebuilding them here.
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 app’s build.gradle.kts dependencies. This library contains the ComposeFeature class with helper functions that simplify using Jetpack Compose in Spatial SDK.
Then register ComposeFeature in your immersive activity:
override fun registerFeatures(): List<SpatialFeature> {
return listOf(..., ComposeFeature())
}
A ComposeView in a view-based panel needs a lifecycle owner, and ComposeFeature supplies one. The view-based registration in the next section attaches its view to that owner, so it requires the feature. The activity-based approach further down does not use it.
Register a view-based panel with Jetpack Compose
The meta-spatial-sdk-compose library includes a special PanelRegistration class called ComposeViewPanelRegistration. You give it the panel’s registration ID, a composeViewCreator function that creates the ComposeView, and a settingsCreator function that creates the panel’s settings. It also accepts an optional panelSetupWithComposeView callback that receives the ComposeView, PanelSceneObject, and Entity.
This example shows how to use ComposeViewPanelRegistration to register a panel and display a ComposeView with your custom Jetpack Compose UI.
Create standalone activities that use Jetpack Compose for more complex UIs that need full activity lifecycle management. This approach does not need ComposeFeature, because the activity owns its own lifecycle. 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 with ActivityPanelRegistration:
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.