1

I am getting "15 Jan - 31 Jan" string when I select this range. But I want date object of start and end date instead of that string.

Screenshot of circled date range picker with a red pen

This is how I show the date 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) {

        }
    }

Here in addOnPositiveButtonClickListener I get that string,

private fun getDateRange(materialCalendarPicker: MaterialDatePicker<out Any>) {
        materialCalendarPicker.addOnPositiveButtonClickListener(
            { selection: Any? ->
                Log.e("DateRangeText",materialCalendarPicker.headerText)
//here i want start and end date
            })
        materialCalendarPicker.addOnNegativeButtonClickListener(
            { dialog: View? ->
            }
        )
        materialCalendarPicker.addOnCancelListener(
            { dialog: DialogInterface? ->
            }
        )
    }
Edric
  • 24,639
  • 13
  • 81
  • 91
Hardik Hirpara
  • 2,594
  • 20
  • 34
  • 1
    In your listener you can use something like: `Pair selection)`. Check the [linked question](https://stackoverflow.com/questions/58931051/materialdatepicker-get-selected-dates/58931200#58931200). – Gabriele Mariotti Jan 09 '20 at 07:51
  • Thanks, @GabrieleMariotti for the reply. But if I do like picker.headerText then I am getting "7 Jan – 23 Jan" like this. And if I do picker.selection.toString then I am getting Pair{1578355200000 1579737600000} like this. – Hardik Hirpara Jan 09 '20 at 09:47
  • And it is correct. `1578355200000` is just the long value of 7 Jan. – Gabriele Mariotti Jan 09 '20 at 09:57
  • Thank you so much @GabrieleMariotti. And are there any things to do for smooth run. Because when I am scroll months vertically then it doesn't seem smooth scrolling. – Hardik Hirpara Jan 09 '20 at 10:18
  • You can only try to use the latest version or open an issue on the [github repo](https://github.com/material-components/material-components-android). – Gabriele Mariotti Jan 09 '20 at 10:45

0 Answers0