DatePicker in Jetpack Compose

How to use DatePicker in Jetpack Compose

Here is the example code to use DatePicker dialog with Jetpack Compose.

In the realm of customization, Jetpack Compose empowers developers with a myriad of options to tailor the DatePicker to suit their application’s theme and branding. From selecting different date formats to customizing the appearance of the calendar, developers wield a palette of tools to imbue the DatePicker with personality and flair. Whether it’s adjusting colors to match a dark theme or incorporating animations for a playful touch, the DatePicker in Jetpack Compose lends itself to limitless creativity.

Moreover, the DatePicker in Jetpack Compose seamlessly integrates with other components, fostering a cohesive and streamlined user experience. Whether it’s coupling it with text fields for date input or incorporating it into dialogs for enhanced functionality, the DatePicker seamlessly aligns with the app’s architecture, enhancing both usability and aesthetics. With its flexible API and robust functionality, the DatePicker serves as a cornerstone in crafting immersive and user-centric applications in the Jetpack Compose ecosystem.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
           val activity = LocalContext.current as AppCompatActivity
                Button(onClick={ showDatePicker(activity)}){
                Text("Date Picker")
           }
        }
    }
}

fun showDatePicker(activity: AppCompatActivity){
    val picker = MaterialDatePicker.Builder.datePicker().build()
    activity?.let {
        picker.show(it.supportFragmentManager, picker.toString())
        picker.addOnPositiveButtonClickListener {
        }
    }
}