Skip to content

Commit 940d737

Browse files
Snippets for /training/wearables/tiles/get_started?version=3 (#704)
* Snippets for /training/wearables/tiles/get_started?version=3 * Tweaks
1 parent 652b999 commit 940d737

File tree

1 file changed

+270
-0
lines changed

1 file changed

+270
-0
lines changed
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
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 android.graphics.Color
21+
import androidx.wear.protolayout.DimensionBuilders.expand
22+
import androidx.wear.protolayout.LayoutElementBuilders.FontSetting.roundness
23+
import androidx.wear.protolayout.LayoutElementBuilders.FontSetting.weight
24+
import androidx.wear.protolayout.LayoutElementBuilders.FontSetting.width
25+
import androidx.wear.protolayout.material3.ButtonDefaults.filledButtonColors
26+
import androidx.wear.protolayout.material3.ButtonDefaults.filledVariantButtonColors
27+
import androidx.wear.protolayout.material3.MaterialScope
28+
import androidx.wear.protolayout.material3.PrimaryLayoutMargins.Companion.MAX_PRIMARY_LAYOUT_MARGIN
29+
import androidx.wear.protolayout.material3.Typography.BODY_MEDIUM
30+
import androidx.wear.protolayout.material3.materialScope
31+
import androidx.wear.protolayout.material3.primaryLayout
32+
import androidx.wear.protolayout.material3.text
33+
import androidx.wear.protolayout.material3.textButton
34+
import androidx.wear.protolayout.material3.textEdgeButton
35+
import androidx.wear.protolayout.modifiers.clickable
36+
import androidx.wear.protolayout.types.argb
37+
import androidx.wear.protolayout.types.layoutString
38+
import androidx.wear.tiles.RequestBuilders
39+
import androidx.wear.tiles.TileService
40+
41+
private fun TileService.materialscope1(
42+
context: Context,
43+
requestParams: RequestBuilders.TileRequest,
44+
myFallbackColorScheme: androidx.wear.protolayout.material3.ColorScheme
45+
) =
46+
// [START android_wear_tile_getstarted_materialscope1]
47+
materialScope(
48+
context = context,
49+
deviceConfiguration =
50+
requestParams.deviceConfiguration, // requestParams is passed to onTileRequest
51+
defaultColorScheme = myFallbackColorScheme
52+
) {
53+
// inside the MaterialScope, you can call functions like primaryLayout()
54+
primaryLayout(
55+
titleSlot = { text(text = "Title".layoutString) },
56+
mainSlot = { text(text = "Main Content".layoutString) },
57+
bottomSlot = {
58+
textEdgeButton(
59+
labelContent = { text("Action".layoutString) },
60+
onClick = clickable()
61+
)
62+
}
63+
)
64+
}
65+
// [END android_wear_tile_getstarted_materialscope1]
66+
67+
private fun TileService.materialscope2(
68+
requestParams: RequestBuilders.TileRequest
69+
) {
70+
// [START android_wear_tile_getstarted_materialscope2]
71+
val myColorScheme =
72+
androidx.wear.protolayout.material3.ColorScheme(
73+
primary = Color.rgb(0, 0, 255).argb, // Blue
74+
onPrimary = Color.rgb(255, 255, 255).argb, // White
75+
// 27 more
76+
)
77+
78+
materialScope(
79+
context = this,
80+
deviceConfiguration = requestParams.deviceConfiguration,
81+
defaultColorScheme = myColorScheme
82+
) {
83+
// If the user selects "no theme" in settings, myColorScheme is used.
84+
// Otherwise, the system-provided theme is used.
85+
// [START_EXCLUDE silent]
86+
text(text = "Placeholder".layoutString)
87+
// [END_EXCLUDE]
88+
}
89+
// [END android_wear_tile_getstarted_materialscope2]
90+
}
91+
92+
private fun MaterialScope.textEdgeButton1() =
93+
// [START android_wear_tile_getstarted_textedgebutton1]
94+
textEdgeButton(
95+
colors =
96+
filledButtonColors()
97+
.copy(
98+
containerColor = colorScheme.tertiary,
99+
labelColor = colorScheme.onTertiary
100+
),
101+
// ... other parameters
102+
// [START_EXCLUDE silent]
103+
onClick = clickable(),
104+
labelContent = { text("Hello, World".layoutString) }
105+
// [END_EXCLUDE]
106+
)
107+
// [END android_wear_tile_getstarted_textedgebutton1]
108+
109+
public fun MaterialScope.textEdgeButton2() =
110+
// [START android_wear_tile_getstarted_textedgebutton2]
111+
textEdgeButton(
112+
colors =
113+
filledButtonColors()
114+
.copy(
115+
containerColor = colorScheme.tertiary,
116+
labelColor = colorScheme.onTertiary
117+
),
118+
// ... other parameters
119+
// [START_EXCLUDE silent]
120+
onClick = clickable(),
121+
labelContent = { text("".layoutString) }
122+
// [END_EXCLUDE]
123+
)
124+
// [END android_wear_tile_getstarted_textedgebutton2]
125+
126+
private fun TileService.materialscope3(
127+
requestParams: RequestBuilders.TileRequest
128+
) {
129+
// [START android_wear_tile_getstarted_materialscope3]
130+
val myColorScheme =
131+
androidx.wear.protolayout.material3.ColorScheme(
132+
primary = Color.rgb(0, 0, 255).argb,
133+
onPrimary = Color.rgb(255, 255, 255).argb,
134+
)
135+
136+
materialScope(
137+
context = this,
138+
deviceConfiguration = requestParams.deviceConfiguration,
139+
allowDynamicTheme = false,
140+
defaultColorScheme = myColorScheme
141+
) {
142+
// myColorScheme is *always* used.
143+
// [START_EXCLUDE silent]
144+
text("Placeholder".layoutString)
145+
// [END_EXCLUDE]
146+
}
147+
// [END android_wear_tile_getstarted_materialscope3]
148+
}
149+
150+
private fun MaterialScope.textEdgeButton5() =
151+
// [START android_wear_tile_getstarted_textedgebutton5]
152+
textEdgeButton(
153+
colors = filledButtonColors(), // default
154+
/* OR colors = filledTonalButtonColors() */
155+
/* OR colors = filledVariantButtonColors() */
156+
// ... other parameters
157+
// [START_EXCLUDE silent]
158+
onClick = clickable(),
159+
labelContent = { text("Action".layoutString) }
160+
// [END_EXCLUDE]
161+
)
162+
// [END android_wear_tile_getstarted_textedgebutton5]
163+
164+
private fun MaterialScope.textEdgeButton6() =
165+
// [START android_wear_tile_getstarted_textedgebutton6]
166+
textEdgeButton(
167+
colors =
168+
filledButtonColors()
169+
.copy(
170+
containerColor = colorScheme.tertiary,
171+
labelColor = colorScheme.onTertiary
172+
),
173+
// ... other parameters
174+
// [START_EXCLUDE silent]
175+
onClick = clickable(),
176+
labelContent = { text("Action".layoutString) }
177+
// [END_EXCLUDE]
178+
)
179+
// [END android_wear_tile_getstarted_textedgebutton6]
180+
181+
private fun MaterialScope.textEdgeButton3() =
182+
// [START android_wear_tile_getstarted_textedgebutton3]
183+
textEdgeButton(
184+
colors =
185+
androidx.wear.protolayout.material3.ButtonColors(
186+
// the materialScope makes colorScheme available
187+
containerColor = colorScheme.secondary,
188+
iconColor = colorScheme.secondaryDim,
189+
labelColor = colorScheme.onSecondary,
190+
secondaryLabelColor = colorScheme.onSecondary
191+
),
192+
// ... other parameters
193+
// [START_EXCLUDE silent]
194+
onClick = clickable(),
195+
labelContent = { text("Action".layoutString) }
196+
// [END_EXCLUDE]
197+
)
198+
// [END android_wear_tile_getstarted_textedgebutton3]
199+
200+
private fun MaterialScope.textEdgeButton4() =
201+
// [START android_wear_tile_getstarted_textedgebutton4]
202+
textEdgeButton(
203+
colors = filledButtonColors().copy(
204+
containerColor = android.graphics.Color.RED.argb, // Using named colors
205+
labelColor = 0xFFFFFF00.toInt().argb // Using a hex code for yellow
206+
),
207+
// ... other parameters
208+
// [START_EXCLUDE silent]
209+
onClick = clickable(),
210+
labelContent = { text("Action".layoutString) }
211+
// [END_EXCLUDE]
212+
)
213+
// [END android_wear_tile_getstarted_textedgebutton4]
214+
215+
private fun MaterialScope.text1() =
216+
// [START android_wear_tile_getstarted_text1]
217+
text(
218+
text = "Hello, World!".layoutString,
219+
typography = BODY_MEDIUM,
220+
)
221+
// [END android_wear_tile_getstarted_text1]
222+
223+
private fun MaterialScope.text2() =
224+
// [START android_wear_tile_getstarted_text2]
225+
text(
226+
text = "Hello, World".layoutString,
227+
italic = true,
228+
229+
// Use elements defined in androidx.wear.protolayout.LayoutElementBuilders.FontSetting
230+
settings =
231+
listOf(
232+
weight(500),
233+
width(100F),
234+
roundness(100)
235+
),
236+
)
237+
// [END android_wear_tile_getstarted_text2]
238+
239+
private fun MaterialScope.textButton1() =
240+
// [START android_wear_tile_getstarted_textbutton1]
241+
textButton(
242+
height = expand(),
243+
width = expand(),
244+
shape = shapes.medium, // OR another value like shapes.full
245+
colors = filledVariantButtonColors(),
246+
labelContent = { text("Hello, World!".layoutString) },
247+
// [START_EXCLUDE silent]
248+
onClick = clickable()
249+
// [END_EXCLUDE]
250+
)
251+
// [END android_wear_tile_getstarted_textbutton1]
252+
253+
private fun MaterialScope.primaryLayout1() =
254+
// [START android_wear_tile_getstarted_primaryLayout1]
255+
primaryLayout(
256+
mainSlot = {
257+
textButton(
258+
shape = shapes.small,
259+
/* ... */
260+
// [START_EXCLUDE silent]
261+
onClick = clickable(),
262+
labelContent = { text("Action".layoutString) },
263+
colors = filledButtonColors()
264+
// [END_EXCLUDE]
265+
)
266+
},
267+
// margin constants defined in androidx.wear.protolayout.material3.PrimaryLayoutMargins
268+
margins = MAX_PRIMARY_LAYOUT_MARGIN,
269+
)
270+
// [END android_wear_tile_getstarted_primaryLayout1]

0 commit comments

Comments
 (0)