Jetpack compose date picker
Date Picker in material design components is a useful component used in an application, the user can select a year, a month, and a day, combining to form date from a calendar-like-looking box. This data is generally needed and collected in applications that require a specific date to book a movie, travel, or hotel booking or filter the data according to a specific date.
Using Date Picker we can not only select a date but also can select a range of dates.
Also Read Alert dialog in jetpack compose
In this article, we will learn how to implement a Date Picker in Android using Jetpack Compose.
Create a New project and select Empty Activity In my case, application name is Coding Bihar you can use any other name.
I used dependency implementation 'androidx.compose.material3:material3:1.1.1'
for Date Picker.
MainActivity
Copy this code →
package com.example.codingbihar
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import com.example.codingbihar.ui.theme.CodingBiharTheme
class MainActivity : ComponentActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CodingBiharTheme {
// A surface container using the 'background' color from the theme
Surface(modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background) {
DatepickerSample()
}
}
}
}
}
Create a separate file for Date Picker and named it as DatepickerSample
DatepickerSample
Copy this code →
package com.example.codingbihar
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import kotlinx.coroutines.launch
import java.time.Instant
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
@RequiresApi(Build.VERSION_CODES.O)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DatepickerSample2() {
// Decoupled snackbar host state from scaffold state for demo purposes.
val snackState = remember { SnackbarHostState() }
val snackScope = rememberCoroutineScope()
SnackbarHost(hostState = snackState, Modifier)
val openDialog = remember { mutableStateOf(true) }
// TODO demo how to read the selected date from the state.
if (openDialog.value) {
val datePickerState = rememberDatePickerState()
// val confirmEnabled = derivedStateOf { datePickerState.selectedDateMillis != null }
DatePickerDialog(
onDismissRequest = {
},
confirmButton = {
TextButton(
onClick = {
openDialog.value = false
snackScope.launch {
val selectedDate = datePickerState.selectedDateMillis?.let {
Instant.ofEpochMilli(it).atOffset(ZoneOffset.UTC)}
snackState.showSnackbar(
"Selected date timestamp: ${selectedDate?.format(DateTimeFormatter.ISO_LOCAL_DATE)}"
)
}
},
) {
Text("OK")
}
},
dismissButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text("Cancel")
}
}
) {
DatePicker(state = datePickerState)
}
}
}
OUTPUT :
DateRangePicker
Copy this code →
package com.example.codingbihar
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.DateRangePicker
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberDateRangePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import kotlinx.coroutines.launch
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DatepickerSample3() {
// Decoupled snackbar host state from scaffold state for demo purposes.
val snackState = remember { SnackbarHostState() }
val snackScope = rememberCoroutineScope()
SnackbarHost(hostState = snackState, Modifier.zIndex(1f))
val state = rememberDateRangePickerState()
Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Top) {
// Add a row with "Save" and dismiss actions.
Row(
modifier = Modifier
.fillMaxWidth()
.padding(start = 12.dp, end = 12.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
IconButton(onClick = { /* dismiss the UI */ }) {
Icon(Icons.Filled.Close, contentDescription = "Localized description")
}
TextButton(
onClick = {
snackScope.launch {
snackState.showSnackbar(
"Saved range (timestamps): " +
"${state.selectedStartDateMillis!!..state.selectedEndDateMillis!!}"
)
}
},
enabled = state.selectedEndDateMillis != null
) {
Text(text = "Save")
}
}
DateRangePicker(state = state, modifier = Modifier.weight(1f))
}
}