1616
1717package com.example.wear.snippets.m3.voiceinput
1818
19- /*
20- * Copyright 2021 The Android Open Source Project
21- *
22- * Licensed under the Apache License, Version 2.0 (the "License");
23- * you may not use this file except in compliance with the License.
24- * You may obtain a copy of the License at
25- *
26- * https://www.apache.org/licenses/LICENSE-2.0
27- *
28- * Unless required by applicable law or agreed to in writing, software
29- * distributed under the License is distributed on an "AS IS" BASIS,
30- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31- * See the License for the specific language governing permissions and
32- * limitations under the License.
33- */
34-
3519import android.content.Intent
3620import android.speech.RecognizerIntent
3721import androidx.activity.compose.rememberLauncherForActivityResult
3822import androidx.activity.result.contract.ActivityResultContracts
39- import androidx.compose.foundation.layout.Arrangement
40- import androidx.compose.foundation.layout.Column
41- import androidx.compose.foundation.layout.fillMaxSize
42- import androidx.compose.foundation.layout.padding
43- import androidx.compose.foundation.rememberScrollState
44- import androidx.compose.foundation.verticalScroll
23+ import androidx.compose.foundation.layout.fillMaxWidth
4524import androidx.compose.runtime.Composable
4625import androidx.compose.runtime.getValue
4726import androidx.compose.runtime.mutableStateOf
4827import androidx.compose.runtime.remember
4928import androidx.compose.runtime.setValue
5029import androidx.compose.ui.Modifier
5130import androidx.compose.ui.res.stringResource
52- import androidx.wear.compose.foundation.rememberActiveFocusRequester
53- import androidx.wear.compose.foundation.rotary.RotaryScrollableDefaults.behavior
54- import androidx.wear.compose.foundation.rotary.rotaryScrollable
31+ import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
32+ import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState
33+ import androidx.wear.compose.material3.AppScaffold
34+ import androidx.wear.compose.material3.Button
35+ import androidx.wear.compose.material3.ScreenScaffold
36+ import androidx.wear.compose.material3.Text
5537import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices
5638import androidx.wear.compose.ui.tooling.preview.WearPreviewFontScales
5739import com.example.wear.R
58- import com.google.android.horologist.annotations.ExperimentalHorologistApi
59- import com.google.android.horologist.compose.layout.AppScaffold
60- import com.google.android.horologist.compose.layout.ScalingLazyColumnDefaults
61- import com.google.android.horologist.compose.layout.ScalingLazyColumnDefaults.ItemType
62- import com.google.android.horologist.compose.layout.ScreenScaffold
63- import com.google.android.horologist.compose.material.Chip
40+ import com.google.android.horologist.compose.layout.ColumnItemType
41+ import com.google.android.horologist.compose.layout.rememberResponsiveColumnPadding
6442
6543/* *
6644 * Shows voice input option
6745 */
68- @OptIn(ExperimentalHorologistApi ::class )
6946@Composable
7047fun VoiceInputScreen () {
7148 AppScaffold {
@@ -83,52 +60,41 @@ fun VoiceInputScreen() {
8360 }
8461 }
8562
86- val scrollState = rememberScrollState()
87-
88- ScreenScaffold (scrollState = scrollState) {
89- // rest of implementation here
90- // [START_EXCLUDE]
91- val padding = ScalingLazyColumnDefaults .padding(
92- first = ItemType .Text ,
93- last = ItemType .Chip
94- )()
95- val focusRequester = rememberActiveFocusRequester()
96- // [END_EXCLUDE]
97- Column (
98- // rest of implementation here
99- // [START_EXCLUDE]
100- modifier = Modifier
101- .fillMaxSize()
102- .verticalScroll(scrollState)
103- .padding(padding)
104- .rotaryScrollable(
105- behavior = behavior(scrollableState = scrollState),
106- focusRequester = focusRequester,
107- ),
108- verticalArrangement = Arrangement .Center
63+ val scrollState = rememberTransformingLazyColumnState()
64+ ScreenScaffold (
65+ scrollState = scrollState,
66+ contentPadding = rememberResponsiveColumnPadding(
67+ first = ColumnItemType .Button
68+ )
69+ ) { contentPadding ->
70+ TransformingLazyColumn (
71+ contentPadding = contentPadding,
72+ state = scrollState,
10973 ) {
110- // [END_EXCLUDE]
111-
112- // Create an intent that can start the Speech Recognizer activity
113- val voiceIntent : Intent = Intent (RecognizerIntent .ACTION_RECOGNIZE_SPEECH ).apply {
114- putExtra(
115- RecognizerIntent .EXTRA_LANGUAGE_MODEL ,
116- RecognizerIntent .LANGUAGE_MODEL_FREE_FORM
117- )
74+ item {
75+ // Create an intent that can start the Speech Recognizer activity
76+ val voiceIntent : Intent =
77+ Intent (RecognizerIntent .ACTION_RECOGNIZE_SPEECH ).apply {
78+ putExtra(
79+ RecognizerIntent .EXTRA_LANGUAGE_MODEL ,
80+ RecognizerIntent .LANGUAGE_MODEL_FREE_FORM
81+ )
11882
119- putExtra(
120- RecognizerIntent .EXTRA_PROMPT ,
121- stringResource(R .string.voice_text_entry_label)
83+ putExtra(
84+ RecognizerIntent .EXTRA_PROMPT ,
85+ stringResource(R .string.voice_text_entry_label)
86+ )
87+ }
88+ // Invoke the process from a Button
89+ Button (
90+ onClick = {
91+ voiceLauncher.launch(voiceIntent)
92+ },
93+ label = { Text (stringResource(R .string.voice_input_label)) },
94+ secondaryLabel = { Text (textForVoiceInput) },
95+ modifier = Modifier .fillMaxWidth()
12296 )
12397 }
124- // Invoke the process from a chip
125- Chip (
126- onClick = {
127- voiceLauncher.launch(voiceIntent)
128- },
129- label = stringResource(R .string.voice_input_label),
130- secondaryLabel = textForVoiceInput
131- )
13298 }
13399 }
134100 // [END android_wear_voice_input]
0 commit comments