Skip to content

Commit 9754666

Browse files
authored
Time picker (#287)
* Adding minimal time picker examples * Fixing region tag * Fixing region tag * Removing wildcard imports * Apply Spotless --------- Co-authored-by: jakeroseman <[email protected]>
1 parent d5cc071 commit 9754666

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/SnippetsActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import com.example.compose.snippets.components.ProgressIndicatorExamples
4242
import com.example.compose.snippets.components.ScaffoldExample
4343
import com.example.compose.snippets.components.SliderExamples
4444
import com.example.compose.snippets.components.SwitchExamples
45+
import com.example.compose.snippets.components.TimePickerExamples
4546
import com.example.compose.snippets.graphics.ApplyPolygonAsClipImage
4647
import com.example.compose.snippets.graphics.BitmapFromComposableSnippet
4748
import com.example.compose.snippets.graphics.BrushExamplesScreen
@@ -103,6 +104,7 @@ class SnippetsActivity : ComponentActivity() {
103104
TopComponentsDestination.DividerExamples -> DividerExamples()
104105
TopComponentsDestination.BadgeExamples -> BadgeExamples()
105106
TopComponentsDestination.PartialBottomSheet -> PartialBottomSheet()
107+
TopComponentsDestination.TimePickerExamples -> TimePickerExamples()
106108
}
107109
}
108110
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Copyright 2024 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.compose.snippets.components
18+
19+
import androidx.compose.foundation.layout.Arrangement
20+
import androidx.compose.foundation.layout.Column
21+
import androidx.compose.foundation.layout.fillMaxSize
22+
import androidx.compose.foundation.layout.padding
23+
import androidx.compose.material3.AlertDialog
24+
import androidx.compose.material3.Button
25+
import androidx.compose.material3.ExperimentalMaterial3Api
26+
import androidx.compose.material3.Text
27+
import androidx.compose.material3.TextButton
28+
import androidx.compose.material3.TimeInput
29+
import androidx.compose.material3.TimePicker
30+
import androidx.compose.material3.TimePickerState
31+
import androidx.compose.material3.rememberTimePickerState
32+
import androidx.compose.runtime.Composable
33+
import androidx.compose.runtime.getValue
34+
import androidx.compose.runtime.mutableStateOf
35+
import androidx.compose.runtime.remember
36+
import androidx.compose.runtime.setValue
37+
import androidx.compose.ui.Alignment
38+
import androidx.compose.ui.Modifier
39+
import androidx.compose.ui.unit.dp
40+
import java.text.SimpleDateFormat
41+
import java.util.Calendar
42+
import java.util.Locale
43+
44+
@OptIn(ExperimentalMaterial3Api::class)
45+
@Composable
46+
fun TimePickerExamples() {
47+
var showDialExample by remember { mutableStateOf(false) }
48+
var showInputExample by remember { mutableStateOf(false) }
49+
50+
var selectedTime: TimePickerState? by remember { mutableStateOf(null) }
51+
52+
val formatter = remember { SimpleDateFormat("hh:mm a", Locale.getDefault()) }
53+
54+
Column(
55+
modifier = Modifier
56+
.fillMaxSize()
57+
.padding(32.dp),
58+
horizontalAlignment = Alignment.CenterHorizontally,
59+
verticalArrangement = Arrangement.spacedBy(24.dp),
60+
) {
61+
Button(onClick = {
62+
showDialExample = true
63+
showInputExample = false
64+
}) {
65+
Text("Dial time picker")
66+
}
67+
Button(onClick = {
68+
showDialExample = false
69+
showInputExample = true
70+
}) {
71+
Text("Input time picker")
72+
}
73+
if (selectedTime != null) {
74+
val cal = Calendar.getInstance()
75+
cal.set(Calendar.HOUR_OF_DAY, selectedTime!!.hour)
76+
cal.set(Calendar.MINUTE, selectedTime!!.minute)
77+
cal.isLenient = false
78+
Text("Selected time = ${formatter.format(cal.time)}")
79+
} else {
80+
Text("No time selected.")
81+
}
82+
}
83+
84+
when {
85+
showDialExample -> DialExample(
86+
onDismiss = { showDialExample = false },
87+
onConfirm = {
88+
time ->
89+
selectedTime = time
90+
showDialExample = false
91+
},
92+
)
93+
showInputExample -> InputExample(
94+
onDismiss = { showInputExample = false },
95+
onConfirm = {
96+
time ->
97+
selectedTime = time
98+
showInputExample = false
99+
},
100+
)
101+
}
102+
}
103+
104+
@OptIn(ExperimentalMaterial3Api::class)
105+
// [START android_compose_components_dial]
106+
@Composable
107+
fun DialExample(
108+
onConfirm: (TimePickerState) -> Unit,
109+
onDismiss: () -> Unit,
110+
) {
111+
val currentTime = Calendar.getInstance()
112+
113+
val timePickerState = rememberTimePickerState(
114+
initialHour = currentTime.get(Calendar.HOUR_OF_DAY),
115+
initialMinute = currentTime.get(Calendar.MINUTE),
116+
is24Hour = true,
117+
)
118+
119+
TimePickerDialog(
120+
onDismiss = { onDismiss() },
121+
onConfirm = { onConfirm(timePickerState) }
122+
) {
123+
TimePicker(
124+
state = timePickerState,
125+
)
126+
}
127+
}
128+
// [END android_compose_components_dial]
129+
130+
@OptIn(ExperimentalMaterial3Api::class)
131+
// [START android_compose_components_input]
132+
@Composable
133+
fun InputExample(
134+
onConfirm: (TimePickerState) -> Unit,
135+
onDismiss: () -> Unit,
136+
) {
137+
val currentTime = Calendar.getInstance()
138+
139+
val timePickerState = rememberTimePickerState(
140+
initialHour = currentTime.get(Calendar.HOUR_OF_DAY),
141+
initialMinute = currentTime.get(Calendar.MINUTE),
142+
is24Hour = true,
143+
)
144+
145+
TimePickerDialog(
146+
onDismiss = { onDismiss() },
147+
onConfirm = { onConfirm(timePickerState) }
148+
) {
149+
TimeInput(
150+
state = timePickerState,
151+
)
152+
}
153+
}
154+
// [END android_compose_components_input]
155+
156+
// [START android_compose_components_timepickerdialog]
157+
@Composable
158+
fun TimePickerDialog(
159+
onDismiss: () -> Unit,
160+
onConfirm: () -> Unit,
161+
content: @Composable () -> Unit
162+
) {
163+
AlertDialog(
164+
onDismissRequest = onDismiss,
165+
dismissButton = {
166+
TextButton(onClick = { onDismiss() }) {
167+
Text("Dismiss")
168+
}
169+
},
170+
confirmButton = {
171+
TextButton(onClick = { onConfirm() }) {
172+
Text("OK")
173+
}
174+
},
175+
text = { content() }
176+
)
177+
}
178+
// [END android_compose_components_timepickerdialog]

compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ enum class TopComponentsDestination(val route: String, val title: String) {
4141
DividerExamples("dividerExamples", "Dividers"),
4242
BadgeExamples("badgeExamples", "Badges"),
4343
PartialBottomSheet("partialBottomSheets", "Partial Bottom Sheet"),
44+
TimePickerExamples("timePickerExamples", "Time Pickers"),
4445
}

0 commit comments

Comments
 (0)