Skip to content

Instantly share code, notes, and snippets.

@prof18
Last active October 17, 2022 08:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prof18/a9186cd59910fab1b0925e7d2a36d226 to your computer and use it in GitHub Desktop.
Save prof18/a9186cd59910fab1b0925e7d2a36d226 to your computer and use it in GitHub Desktop.
A simple Date Picker for Jetpack Compose, waiting for the official one
import androidx.compose.foundation.Text
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import java.util.*
fun getYearList() = (2015..2023).map { it.toString() }
fun getMonthList() = (1..12).map { it.toString() }
fun getDayList() = (1..31).map { it.toString() }
@Composable
fun DatePickerDialog(
showDialog: Boolean,
setDialogVisible: (Boolean) -> Unit
) {
if (showDialog) {
AlertDialog(
onDismissRequest = {
setDialogVisible(false)
},
title = {
Text(text = "Select a date")
},
text = {
Row(
modifier = Modifier.fillMaxWidth()
) {
DatePickerItemDropdownMenu(
initialText = Calendar.getInstance().get(Calendar.YEAR).toString(),
itemList = getYearList(),
onItemSelected = {
// TODO: send data to view model
}
)
Spacer(modifier = Modifier.width(16.dp))
DatePickerItemDropdownMenu(
initialText = (Calendar.getInstance().get(Calendar.MONTH) + 1).toString(),
itemList = getMonthList(),
onItemSelected = {
// TODO: send data to view model
}
)
Spacer(modifier = Modifier.width(16.dp))
DatePickerItemDropdownMenu(
initialText = Calendar.getInstance().get(Calendar.DAY_OF_MONTH).toString(),
itemList = getDayList(),
onItemSelected = {
// TODO: send data to view model
}
)
}
},
confirmButton = {
Button(onClick = {
// TODO: send info to viewModel
setDialogVisible(false)
}) {
Text("Confirm")
}
},
dismissButton = {
Button(onClick = {
setDialogVisible(false)
}) {
Text("Cancel")
}
}
)
}
}
@Composable
private fun DatePickerItemDropdownMenu(
initialText: String,
itemList: List<String>,
onItemSelected: (String) -> Unit,
) {
val (dropdownText, setDropdownText) = remember { mutableStateOf(initialText) }
val (isMenuExpanded, setMenuExpanded) = remember { mutableStateOf(false) }
DropdownMenu(
toggle = {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.clickable(onClick = {
setMenuExpanded(true)
})
) {
Text(
text = dropdownText,
style = MaterialTheme.typography.body1
)
Spacer(Modifier.width(4.dp))
Icon(
modifier = Modifier.size(16.dp),
asset = Icons.Filled.KeyboardArrowDown
)
}
},
expanded = isMenuExpanded,
onDismissRequest = {
setMenuExpanded(false)
}
) {
itemList.forEach {
DropdownMenuItem(onClick = {
setDropdownText(it)
setMenuExpanded(false)
onItemSelected(it)
}) {
Text(
text = it,
style = MaterialTheme.typography.body1,
)
}
}
}
}
@mairs8
Copy link

mairs8 commented Oct 16, 2022

Is there an official one from Compose yet? I am looking for one without the DialogBox wrapped around it (trying to embed within my own Dialog). Thanks

@prof18
Copy link
Author

prof18 commented Oct 17, 2022

Is there an official one from Compose yet? I am looking for one without the DialogBox wrapped around it (trying to embed within my own Dialog). Thanks

I don't think there's a native component yet. You have to wrap the View System DatePickerDialog.
I haven't tried, but this could be an approach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment