Skip to content

Commit 7e66d47

Browse files
committed
Migrate snippets from "Develop UI for Android Views-based Apps"
1 parent da39e1f commit 7e66d47

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.compose
18+
19+
import android.content.Context
20+
import android.os.Bundle
21+
import android.view.LayoutInflater
22+
import android.view.View
23+
import android.view.ViewGroup
24+
import androidx.activity.ComponentActivity
25+
import androidx.compose.material3.Text
26+
import androidx.compose.ui.platform.ComposeView
27+
import androidx.compose.ui.platform.ViewCompositionStrategy
28+
import androidx.compose.ui.unit.dp
29+
import androidx.fragment.app.Fragment
30+
import androidx.xr.compose.platform.setSubspaceContent
31+
import androidx.xr.compose.subspace.SpatialPanel
32+
import androidx.xr.compose.subspace.layout.SubspaceModifier
33+
import androidx.xr.compose.subspace.layout.depth
34+
import androidx.xr.compose.subspace.layout.height
35+
import androidx.xr.compose.subspace.layout.width
36+
import androidx.xr.scenecore.Dimensions
37+
import androidx.xr.scenecore.PanelEntity
38+
import androidx.xr.scenecore.Session
39+
import com.example.xr.R
40+
41+
private class MyCustomView(context: Context) : View(context)
42+
43+
private class ActivityWithSubspaceContent : ComponentActivity() {
44+
override fun onCreate(savedInstanceState: Bundle?) {
45+
super.onCreate(savedInstanceState)
46+
// [START androidxr_compose_ActivityWithSubspaceContent]
47+
setSubspaceContent {
48+
SpatialPanel(
49+
view = MyCustomView(this),
50+
modifier = SubspaceModifier.height(500.dp).width(500.dp).depth(25.dp)
51+
)
52+
}
53+
// [END androidxr_compose_ActivityWithSubspaceContent]
54+
}
55+
}
56+
57+
private class FragmentWithComposeView() : Fragment() {
58+
// [START androidxr_compose_FragmentWithComposeView]
59+
override fun onCreateView(
60+
inflater: LayoutInflater,
61+
container: ViewGroup?,
62+
savedInstanceState: Bundle?
63+
): View {
64+
val view = inflater.inflate(R.layout.example_fragment, container, false)
65+
view.findViewById<ComposeView>(R.id.compose_view).apply {
66+
// Dispose of the Composition when the view's LifecycleOwner
67+
// is destroyed
68+
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
69+
setContent {
70+
// In Compose world
71+
SpatialPanel(SubspaceModifier.height(500.dp).width(500.dp)) {
72+
Text("Spatial Panel with Orbiter")
73+
}
74+
}
75+
}
76+
return view
77+
}
78+
// [END androidxr_compose_FragmentWithComposeView]
79+
}
80+
81+
fun ComponentActivity.PanelEntityWithView(xrSession: Session) {
82+
// [START androidxr_compose_PanelEntityWithView]
83+
val panelContent = MyCustomView(this)
84+
val panelEntity = PanelEntity.create(
85+
session = xrSession,
86+
view = panelContent,
87+
surfaceDimensionsPx = Dimensions(500f, 500f),
88+
dimensions = Dimensions(1f, 1f, 1f),
89+
name = "panel entity"
90+
)
91+
// [END androidxr_compose_PanelEntityWithView]
92+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
<androidx.compose.ui.platform.ComposeView
6+
android:id="@+id/compose_view"
7+
android:layout_width="wrap_content"
8+
android:layout_height="wrap_content" />
9+
</LinearLayout>

0 commit comments

Comments
 (0)