Skip to content

Commit a015aef

Browse files
Add snippets for /training/wearables/tiles/versioning (#699)
1 parent 906dee1 commit a015aef

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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.wear.snippets.tile
18+
19+
import android.content.Context
20+
import androidx.wear.protolayout.DeviceParametersBuilders
21+
import androidx.wear.protolayout.DimensionBuilders.expand
22+
import androidx.wear.protolayout.LayoutElementBuilders.Column
23+
import androidx.wear.protolayout.ModifiersBuilders
24+
import androidx.wear.protolayout.TypeBuilders
25+
import androidx.wear.protolayout.material.Text
26+
import androidx.wear.protolayout.material.Typography
27+
import androidx.wear.protolayout.material.layouts.PrimaryLayout
28+
import androidx.wear.protolayout.material3.MaterialScope
29+
import androidx.wear.protolayout.material3.materialScope
30+
import androidx.wear.protolayout.material3.primaryLayout
31+
import androidx.wear.protolayout.material3.text
32+
import androidx.wear.protolayout.modifiers.LayoutModifier
33+
import androidx.wear.protolayout.modifiers.opacity
34+
import androidx.wear.protolayout.types.layoutString
35+
36+
class LayoutM2 {
37+
// [START android_wear_tile_version_layoutm2]
38+
fun myLayout(
39+
context: Context,
40+
deviceConfiguration: DeviceParametersBuilders.DeviceParameters
41+
) =
42+
PrimaryLayout.Builder(deviceConfiguration)
43+
.setResponsiveContentInsetEnabled(true)
44+
.setContent(
45+
Text.Builder(context, "Hello World!")
46+
.setTypography(Typography.TYPOGRAPHY_BODY1)
47+
.build()
48+
)
49+
.build()
50+
// [END android_wear_tile_version_layoutm2]
51+
52+
// [START android_wear_tile_version_modifierm2]
53+
// Uses Builder-style modifier to set opacity
54+
fun myModifier(): ModifiersBuilders.Modifiers =
55+
ModifiersBuilders.Modifiers.Builder()
56+
.setOpacity(TypeBuilders.FloatProp.Builder(0.5F).build())
57+
.build()
58+
// [END android_wear_tile_version_modifierm2]
59+
}
60+
61+
class LayoutM3 {
62+
// [START android_wear_tile_version_layoutm3]
63+
fun myLayout(
64+
context: Context,
65+
deviceConfiguration: DeviceParametersBuilders.DeviceParameters,
66+
) =
67+
materialScope(context, deviceConfiguration) {
68+
primaryLayout(mainSlot = { text("Hello, World!".layoutString) }) // HELPME
69+
}
70+
// [END android_wear_tile_version_layoutm3]
71+
72+
// [START android_wear_tile_version_modifierm3]
73+
// Uses Compose-like modifiers to set opacity
74+
fun myModifier(): LayoutModifier = LayoutModifier.opacity(0.5F)
75+
// [END android_wear_tile_version_modifierm3]
76+
}
77+
78+
private fun MaterialScope.withoutHelpers() {
79+
// [START android_wear_tile_version_layoutwithouthelpers]
80+
primaryLayout(
81+
mainSlot = {
82+
Column.Builder()
83+
.setWidth(expand())
84+
.setHeight(expand())
85+
.addContent(text("A".layoutString))
86+
.addContent(text("B".layoutString))
87+
.addContent(text("C".layoutString))
88+
.build()
89+
}
90+
)
91+
// [END android_wear_tile_version_layoutwithouthelpers]
92+
}
93+
94+
private fun MaterialScope.withHelpers() {
95+
// [START android_wear_tile_version_layoutwithhelpers]
96+
// Function literal with receiver helper function
97+
fun column(builder: Column.Builder.() -> Unit) =
98+
Column.Builder().apply(builder).build()
99+
100+
primaryLayout(
101+
mainSlot = {
102+
column {
103+
setWidth(expand())
104+
setHeight(expand())
105+
addContent(text("A".layoutString))
106+
addContent(text("B".layoutString))
107+
addContent(text("C".layoutString))
108+
}
109+
}
110+
)
111+
// [END android_wear_tile_version_layoutwithhelpers]
112+
}

0 commit comments

Comments
 (0)