How to use Date Picker in Jetpack Compose

Β·

1 min read

A few days ago I was working on my project and at that moment I came across a problem how can I use the date picker in compose?

So, google it and find and came across a solution which fine and you can modify it according to your needs in your project.

package com.geekaid.sdejobs.ui.screens

import android.app.DatePickerDialog
import android.widget.DatePicker
import androidx.compose.material.OutlinedButton
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.platform.LocalContext
import java.util.*

@Composable
fun Calender() {

var datePicked by remember { mutableStateOf("1") }

val context = LocalContext.current
    val year: Int
    val month: Int
    val day: Int

val calendar = Calendar.getInstance()
    year = calendar.get(Calendar.YEAR)
    month = calendar.get(Calendar.MONTH)
    day = calendar.get(Calendar.DAY_OF_MONTH)
    calendar.time = Date()

val datePickerDialog = DatePickerDialog(
        context,
        { _: DatePicker, year: Int, month: Int, dayOfMonth: Int ->
            datePicked = "$dayOfMonth/$month/$year"
        }, year, month, day
    )

OutlinedButton(onClick = { datePickerDialog.show() }) {
        Text(text = "Pick Date")
    }
}
Β