# DurationPicker

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

## API Reference

### DurationPicker

> Source set: Android

```kotlin
@Composable
public fun DurationPicker(
    initialDuration: Duration,
    onDurationPicked: (Duration) -> Unit,
    modifier: Modifier = Modifier,
    minValidDuration: Duration? = null,
    maxValidDuration: Duration? = null,
    durationPickerType: DurationPickerType = DurationPickerDefaults.type,
    initialSelection: DurationPickerSelection =
        DurationPickerDefaults.initialSelection(durationPickerType),
    colors: DurationPickerColors = DurationPickerDefaults.colors(),
)
```

#### Parameters

| | |
| --- | --- |
| initialDuration | The initial duration to be displayed in the DurationPicker. Must be non-negative and not exceed the limit for [durationPickerType](https://composables.com/jetpack-compose/androidx.wear.compose/compose-material3/classes/DurationPickerType/api.md). Changing `initialDuration` after the first composition does not reset the picker. Changing `minValidDuration`, `maxValidDuration` or [durationPickerType](https://composables.com/jetpack-compose/androidx.wear.compose/compose-material3/classes/DurationPickerType/api.md) resets the picker to `initialDuration` and `initialSelection`. |
| onDurationPicked | The callback that is called when the user confirms the duration selection. It provides the selected duration as `Duration`. Any units not displayed in the picker (e.g. seconds for `DurationPickerType.HoursMinutes`) will be 0 in the returned `Duration`. |
| modifier | Modifier to be applied to the `Box` containing the UI elements. |
| minValidDuration | The minimum valid duration that can be confirmed. Must be non-negative and not exceed `maxValidDuration`. See [DurationPickerType](https://composables.com/jetpack-compose/androidx.wear.compose/compose-material3/classes/DurationPickerType/api.md) for default values. |
| maxValidDuration | The maximum valid duration that can be confirmed. Must be non-negative and not exceed the limit for [durationPickerType](https://composables.com/jetpack-compose/androidx.wear.compose/compose-material3/classes/DurationPickerType/api.md). When null, the default is the max(`minValidDuration`, [durationPickerType](https://composables.com/jetpack-compose/androidx.wear.compose/compose-material3/classes/DurationPickerType/api.md)'s default maximum), up to the [durationPickerType](https://composables.com/jetpack-compose/androidx.wear.compose/compose-material3/classes/DurationPickerType/api.md)'s limit. See [DurationPickerType](https://composables.com/jetpack-compose/androidx.wear.compose/compose-material3/classes/DurationPickerType/api.md) for default values. |
| durationPickerType | The different [DurationPickerType](https://composables.com/jetpack-compose/androidx.wear.compose/compose-material3/classes/DurationPickerType/api.md) supported by this duration picker. |
| initialSelection | The initial component to be selected. If `initialSelection` is a column that is not shown for the given [durationPickerType](https://composables.com/jetpack-compose/androidx.wear.compose/compose-material3/classes/DurationPickerType/api.md) (for example, `DurationPickerSelection.Seconds` with `DurationPickerType.HoursMinutes`), the first column is selected instead. If `initialSelection` is the confirm button but the displayed duration is invalid, the confirm button is disabled, so the first column is selected instead. When touch exploration services (such as TalkBack) are enabled, no component is selected initially. |
| colors | [DurationPickerColors](https://composables.com/jetpack-compose/androidx.wear.compose/compose-material3/classes/DurationPickerColors/api.md) to be applied to the DurationPicker. |

## Code Examples

### DurationPickerHoursMinutesSecondsSample

```kotlin
@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

```kotlin
@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

```kotlin
@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

```kotlin
@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") },
            )
        }
    }
}
```
