1

I am migrating the calendar components to the new Materials library, MaterialDatePicker, however I am having problems in disabling the cancel button.

My use case is that user cannot dismiss the DatePicker Dialog without selecting the date.

MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
builder.setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR);

TypedValue typedValue = new TypedValue();
if (activity.getTheme().resolveAttribute(R.attr.materialCalendarTheme, typedValue, true)) {
     builder.setTheme(typedValue.data);
}

CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
builder.setCalendarConstraints(constraintsBuilder.build());

MaterialDatePicker<Pair<Long, Long>> picker = builder.build();
picker.setCancelable(false); // Not working
picker.show(((AppCompatActivity) activity).getSupportFragmentManager(), picker.toString());
Ankit
  • 95
  • 1
  • 6
  • Please have a look at this [answer](https://stackoverflow.com/a/19274977/7948109), you can create a custom datePicker to remove `cancel` button – Rahul Gaur Feb 11 '20 at 05:17
  • 1
    Force `setCancelable` to `false` doesn't disable the **Cancel** button rather it restrict picker dialog to auto hide in outside click – Md. Asaduzzaman Feb 11 '20 at 05:42

1 Answers1

-1

Try one these workarounds:

        picker?.view?.findViewById<Button>(com.google.android.material.R.id.cancel_button)?.isEnabled = false
        picker?.view?.findViewById<Button>(com.google.android.material.R.id.cancel_button)?.visibility = View.GONE