I need to design a custom calendar to choose dates while swiping over them. I need some solution on how to implement swipe on the calendar and also will be helpfull if there are any libraries which support this feature.
Any type of help will be considered. thank you! The swipe date UI image is as shown
With the Default Material Components for Android, You can use the new MaterialDatePicker. In which you can get a single date selection as well as date range selection.
add the following to your build.gradle
implementation 'com.google.android.material:material:1.2.0 alpha02'
Call below method for open date range picker
private fun setupRangePickerDialog() {
val builder:MaterialDatePicker.Builder<*> =MaterialDatePicker.Builder.dateRangePicker()
val constraintsBuilder = CalendarConstraints.Builder()
try {
builder.setCalendarConstraints(constraintsBuilder.build())
val picker:MaterialDatePicker<*> =builder.build()
getDateRange(picker)
picker.show(supportFragmentManager, picker.toString())
} catch (e:IllegalArgumentException){
}
}
Now put below method for getting start and end date
private fun getDateRange(materialCalendarPicker:MaterialDatePicker<out Any>) {
materialCalendarPicker.addOnPositiveButtonClickListener({
selection:Any ? ->
Log.e("DateRangeText", materialCalendarPicker.headerText)})
materialCalendarPicker.addOnNegativeButtonClickListener({
dialog:View ? ->})
materialCalendarPicker.addOnCancelListener({
dialog:DialogInterface ? ->})
}
Congrats, You are done!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With