16
16
17
17
package com.example.compose.snippets.components
18
18
19
+ import androidx.compose.foundation.background
19
20
import androidx.compose.foundation.layout.Arrangement
20
21
import androidx.compose.foundation.layout.Column
22
+ import androidx.compose.foundation.layout.IntrinsicSize
23
+ import androidx.compose.foundation.layout.Row
24
+ import androidx.compose.foundation.layout.Spacer
21
25
import androidx.compose.foundation.layout.fillMaxSize
26
+ import androidx.compose.foundation.layout.fillMaxWidth
27
+ import androidx.compose.foundation.layout.height
22
28
import androidx.compose.foundation.layout.padding
29
+ import androidx.compose.foundation.layout.width
30
+ import androidx.compose.material.icons.Icons
31
+ import androidx.compose.material.icons.filled.AccessTime
32
+ import androidx.compose.material.icons.filled.EditCalendar
23
33
import androidx.compose.material3.AlertDialog
24
34
import androidx.compose.material3.Button
25
35
import androidx.compose.material3.ExperimentalMaterial3Api
36
+ import androidx.compose.material3.Icon
37
+ import androidx.compose.material3.IconButton
38
+ import androidx.compose.material3.MaterialTheme
39
+ import androidx.compose.material3.Surface
26
40
import androidx.compose.material3.Text
27
41
import androidx.compose.material3.TextButton
28
42
import androidx.compose.material3.TimeInput
@@ -37,6 +51,8 @@ import androidx.compose.runtime.setValue
37
51
import androidx.compose.ui.Alignment
38
52
import androidx.compose.ui.Modifier
39
53
import androidx.compose.ui.unit.dp
54
+ import androidx.compose.ui.window.Dialog
55
+ import androidx.compose.ui.window.DialogProperties
40
56
import java.text.SimpleDateFormat
41
57
import java.util.Calendar
42
58
import java.util.Locale
@@ -46,6 +62,7 @@ import java.util.Locale
46
62
fun TimePickerExamples () {
47
63
var showDialExample by remember { mutableStateOf(false ) }
48
64
var showInputExample by remember { mutableStateOf(false ) }
65
+ var showAdvancedExample by remember { mutableStateOf(false ) }
49
66
50
67
var selectedTime: TimePickerState ? by remember { mutableStateOf(null ) }
51
68
@@ -60,16 +77,19 @@ fun TimePickerExamples() {
60
77
) {
61
78
Button (onClick = {
62
79
showDialExample = true
63
- showInputExample = false
64
80
}) {
65
81
Text (" Dial time picker" )
66
82
}
67
83
Button (onClick = {
68
- showDialExample = false
69
84
showInputExample = true
70
85
}) {
71
86
Text (" Input time picker" )
72
87
}
88
+ Button (onClick = {
89
+ showAdvancedExample = true
90
+ }) {
91
+ Text (" Time picker with custom dialog" )
92
+ }
73
93
if (selectedTime != null ) {
74
94
val cal = Calendar .getInstance()
75
95
cal.set(Calendar .HOUR_OF_DAY , selectedTime!! .hour)
@@ -98,6 +118,14 @@ fun TimePickerExamples() {
98
118
showInputExample = false
99
119
},
100
120
)
121
+ showAdvancedExample -> AdvancedTimePickerExample (
122
+ onDismiss = { showAdvancedExample = false },
123
+ onConfirm = {
124
+ time ->
125
+ selectedTime = time
126
+ showAdvancedExample = false
127
+ },
128
+ )
101
129
}
102
130
}
103
131
@@ -153,6 +181,57 @@ fun InputExample(
153
181
}
154
182
// [END android_compose_components_input]
155
183
184
+ @OptIn(ExperimentalMaterial3Api ::class )
185
+ // [START android_compose_components_advanced]
186
+ @Composable
187
+ fun AdvancedTimePickerExample (
188
+ onConfirm : (TimePickerState ) -> Unit ,
189
+ onDismiss : () -> Unit ,
190
+ ) {
191
+
192
+ val currentTime = Calendar .getInstance()
193
+
194
+ val timePickerState = rememberTimePickerState(
195
+ initialHour = currentTime.get(Calendar .HOUR_OF_DAY ),
196
+ initialMinute = currentTime.get(Calendar .MINUTE ),
197
+ is24Hour = true ,
198
+ )
199
+
200
+ /* * Determines whether the time picker is dial or input */
201
+ var showDial by remember { mutableStateOf(true ) }
202
+
203
+ /* * The icon used for the icon button that switches from dial to input */
204
+ val toggleIcon = if (showDial) {
205
+ Icons .Filled .EditCalendar
206
+ } else {
207
+ Icons .Filled .AccessTime
208
+ }
209
+
210
+ AdvancedTimePickerDialog (
211
+ onDismiss = { onDismiss() },
212
+ onConfirm = { onConfirm(timePickerState) },
213
+ toggle = {
214
+ IconButton (onClick = { showDial = ! showDial }) {
215
+ Icon (
216
+ imageVector = toggleIcon,
217
+ contentDescription = " Time picker type toggle" ,
218
+ )
219
+ }
220
+ },
221
+ ) {
222
+ if (showDial) {
223
+ TimePicker (
224
+ state = timePickerState,
225
+ )
226
+ } else {
227
+ TimeInput (
228
+ state = timePickerState,
229
+ )
230
+ }
231
+ }
232
+ }
233
+ // [END android_compose_components_advanced]
234
+
156
235
// [START android_compose_components_timepickerdialog]
157
236
@Composable
158
237
fun TimePickerDialog (
@@ -176,3 +255,56 @@ fun TimePickerDialog(
176
255
)
177
256
}
178
257
// [END android_compose_components_timepickerdialog]
258
+
259
+ // [START android_compose_components_advanceddialog]
260
+ @Composable
261
+ fun AdvancedTimePickerDialog (
262
+ title : String = "Select Time ",
263
+ onDismiss : () -> Unit ,
264
+ onConfirm : () -> Unit ,
265
+ toggle : @Composable () -> Unit = {},
266
+ content : @Composable () -> Unit ,
267
+ ) {
268
+ Dialog (
269
+ onDismissRequest = onDismiss,
270
+ properties = DialogProperties (usePlatformDefaultWidth = false ),
271
+ ) {
272
+ Surface (
273
+ shape = MaterialTheme .shapes.extraLarge,
274
+ tonalElevation = 6 .dp,
275
+ modifier =
276
+ Modifier
277
+ .width(IntrinsicSize .Min )
278
+ .height(IntrinsicSize .Min )
279
+ .background(
280
+ shape = MaterialTheme .shapes.extraLarge,
281
+ color = MaterialTheme .colorScheme.surface
282
+ ),
283
+ ) {
284
+ Column (
285
+ modifier = Modifier .padding(24 .dp),
286
+ horizontalAlignment = Alignment .CenterHorizontally
287
+ ) {
288
+ Text (
289
+ modifier = Modifier
290
+ .fillMaxWidth()
291
+ .padding(bottom = 20 .dp),
292
+ text = title,
293
+ style = MaterialTheme .typography.labelMedium
294
+ )
295
+ content()
296
+ Row (
297
+ modifier = Modifier
298
+ .height(40 .dp)
299
+ .fillMaxWidth()
300
+ ) {
301
+ toggle()
302
+ Spacer (modifier = Modifier .weight(1f ))
303
+ TextButton (onClick = onDismiss) { Text (" Cancel" ) }
304
+ TextButton (onClick = onConfirm) { Text (" OK" ) }
305
+ }
306
+ }
307
+ }
308
+ }
309
+ }
310
+ // [END android_compose_components_advanceddialog]
0 commit comments