Compose Component

DurationPicker

A full screen DurationPicker that allows users to select a duration.

DurationPicker social preview

DurationPickerHoursMinutesSecondsSample

@RequiresApi(Build.VERSION_CODES.O)
@Sampled
@Composable
fun DurationPickerHoursMinutesSecondsSample() {
    var showDurationPicker by remember { mutableStateOf(true) }
    var duration by remember { mutableStateOf(Duration.ofHours(1).plusMinutes(30)) }

    if (showDurationPicker) {
        DurationPicker(
            initialDuration = duration,
            onDurationPicked = {
                duration = it
                showDurationPicker = false
            },
            durationPickerType = DurationPickerType.HoursMinutesSeconds,
        )
    } else {
        Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
            val hours = duration.toHours()
            val minutes = duration.toMinutes() % 60
            val seconds = duration.seconds % 60
            val labelText =
                "%02d:%02d:%02d".format(LocalLocale.current.platformLocale, hours, minutes, seconds)

            Button(
                onClick = { showDurationPicker = true },
                label = { Text("Selected Duration") },
                secondaryLabel = { Text(labelText) },
                icon = { Icon(imageVector = Icons.Filled.Edit, contentDescription = "Edit") },
            )
        }
    }
}

DurationPickerMinutesSecondsSample

@RequiresApi(Build.VERSION_CODES.O)
@Sampled
@Composable
fun DurationPickerMinutesSecondsSample() {
    var showDurationPicker by remember { mutableStateOf(true) }
    var duration by remember { mutableStateOf(Duration.ofMinutes(5)) }

    if (showDurationPicker) {
        DurationPicker(
            initialDuration = duration,
            onDurationPicked = {
                duration = it
                showDurationPicker = false
            },
            durationPickerType = DurationPickerType.MinutesSeconds,
        )
    } else {
        Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
            val minutes = duration.toMinutes()
            val seconds = duration.seconds % 60
            val labelText = "%02d:%02d".format(LocalLocale.current.platformLocale, minutes, seconds)

            Button(
                onClick = { showDurationPicker = true },
                label = { Text("Selected Duration") },
                secondaryLabel = { Text(labelText) },
                icon = { Icon(imageVector = Icons.Filled.Edit, contentDescription = "Edit") },
            )
        }
    }
}

DurationPickerHoursMinutesSample

@RequiresApi(Build.VERSION_CODES.O)
@Sampled
@Composable
fun DurationPickerHoursMinutesSample() {
    var showDurationPicker by remember { mutableStateOf(true) }
    var duration by remember { mutableStateOf(Duration.ofHours(2)) }

    if (showDurationPicker) {
        DurationPicker(
            initialDuration = duration,
            onDurationPicked = {
                duration = it
                showDurationPicker = false
            },
            durationPickerType = DurationPickerType.HoursMinutes,
        )
    } else {
        Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
            val hours = duration.toHours()
            val minutes = duration.toMinutes() % 60
            val labelText = "%02d:%02d".format(LocalLocale.current.platformLocale, hours, minutes)

            Button(
                onClick = { showDurationPicker = true },
                label = { Text("Selected Duration") },
                secondaryLabel = { Text(labelText) },
                icon = { Icon(imageVector = Icons.Filled.Edit, contentDescription = "Edit") },
            )
        }
    }
}

DurationPickerMaximumDurationSample

@RequiresApi(Build.VERSION_CODES.O)
@Sampled
@Composable
fun DurationPickerMaximumDurationSample() {
    var showDurationPicker by remember { mutableStateOf(true) }
    var duration by remember { mutableStateOf(Duration.ofHours(36).plusMinutes(45)) }

    if (showDurationPicker) {
        DurationPicker(
            initialDuration = duration,
            onDurationPicked = {
                duration = it
                showDurationPicker = false
            },
            maxValidDuration = Duration.ofHours(45).plusMinutes(30),
            durationPickerType = DurationPickerType.HoursMinutes,
        )
    } else {
        Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
            val hours = duration.toHours()
            val minutes = duration.toMinutes() % 60
            val labelText = "%02d:%02d".format(LocalLocale.current.platformLocale, hours, minutes)

            Button(
                onClick = { showDurationPicker = true },
                label = { Text("Selected Duration") },
                secondaryLabel = { Text(labelText) },
                icon = { Icon(imageVector = Icons.Filled.Edit, contentDescription = "Edit") },
            )
        }
    }
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: