Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Calendar Date range swipe selection android

I need to design a custom calendar to choose dates whileenter image description here 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

like image 539
Sachin K Pissay Avatar asked Mar 21 '16 10:03

Sachin K Pissay


1 Answers

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!

like image 175
Hardik Hirpara Avatar answered Sep 23 '22 20:09

Hardik Hirpara