question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[MaterialDatePicker] API to disable dialog auto dismiss

See original GitHub issue

Is your feature request related to a problem? Please describe. I use a MaterialDatePicker.Builder.dateRangePicker(). When the user clicks the save button, the dialog is automatically dismissed. I don’t want that, in my use case I need the dialog to be dismissed by the user with the back button or programatically.

Describe the solution you’d like I request an official API to disable auto dismiss when save button is clicked. Something similar to this. On AlertDialog, we can get the positive button and set the on click listener on it, thus preventing the default behavior.

Describe alternatives you’ve considered I made a hack:

val picker=MaterialDatePicker.Builder.dateRangePicker()
    .build()

Handler().postDelayed({
    //hack - disable dismiss on confirm button click
    val confirmButton=picker::class.java.declaredFields
        .find { field -> field.name=="confirmButton" }
        ?.let { field ->
            field.isAccessible=true
            field.get(picker) as? Button
        }

    confirmButton?.setOnClickListener {
        val interval=picker.selection as Pair<Long?, Long?>
        doSomething(interval)
    }
}, 1000)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
drchencommented, Oct 6, 2021

I think the recommended practice here will be overriding the dismiss() method and decide if you want to dismiss the picker by setting a flag in button listeners.

Another option is you can use findViewById(R.id.confirm_button) instead of reflection, which is less prone to the internal change of the library.

I’ll close the request for now since it’s an intended behavior and there is a reliable way to bypass it. : )

1reaction
andobcommented, Feb 21, 2021

I always used that hack with reflection as a workaround. regarding your issue, I guess you could use the same hack:

  • grab confirmButton using reflection
  • when date is selected: set confirmButton.isEnabled to true/false
Read more comments on GitHub >

github_iconTop Results From Across the Web

Disable Cancel button in Android's MaterialDatePicker
My use case is that user cannot dismiss the DatePicker Dialog without selecting the date. MaterialDatePicker.
Read more >
MaterialDatePicker - Android Developers
A Dialog with a header, MaterialCalendar, and set of actions. ... whenever the DialogFragment is dismissed, no matter how it is dismissed.
Read more >
DatePicker API - Material-UI Pickers
Name Type Default onChange * (date: DateIOType) => void value * ParsableDate allowKeyboardControl boolean true
Read more >
Datepicker | Angular Material
API reference for Angular Material datepicker ... Component responsible for managing the date range picker popup/dialog. Selector: mat-date-range-picker.
Read more >
showDatePicker function - material library - Dart API
Shows a dialog containing a Material Design date picker. ... This sample demonstrates how to create a restorable Material date picker.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found