Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MaterialDatePicker get selected dates

I'm calling a MaterialDatePicker like this in Android:

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

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

int dialogTheme = resolveOrThrow(getContext(), R.attr.materialCalendarTheme);
builder.setTheme(dialogTheme);

MaterialDatePicker<?> picker = builder.build();

picker.show(getFragmentManager(), picker.toString());

the library is:

dependencies {
    implementation 'com.google.android.material:material:1.2.0-alpha01'
}

How can I get the selected date of this calendar? I can't find any listener like onDateSet or OnDateSetListener

like image 307
Erjon Avatar asked Nov 19 '19 09:11

Erjon


3 Answers

The answer of GR Envoy is good, but I want to change a bit. It would be better to set time zone to UTC.

private val outputDateFormat = SimpleDateFormat("dd.MM.yyyy", Locale.getDefault()).apply {
    timeZone = TimeZone.getTimeZone("UTC")
}

...
picker.addOnPositiveButtonClickListener {
    val text = outputDateFormat.format(it)
}
like image 100
CoolMind Avatar answered Oct 20 '22 19:10

CoolMind


Just use the addOnPositiveButtonClickListener listener called when the user confirms a valid selection:

For a single date picker:

picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
      @Override public void onPositiveButtonClick(Long selection) {
        // Do something...
        //Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        //calendar.setTimeInMillis(selection);   

      }
    });

For a range date picker:

MaterialDatePicker<Pair<Long, Long>> pickerRange = builderRange.build();
pickerRange.show(....);

pickerRange.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
  @Override public void onPositiveButtonClick(Pair<Long,Long> selection) {
       Long startDate = selection.first;
       Long endDate = selection.second;
       //Do something...
  }
});
like image 30
Gabriele Mariotti Avatar answered Oct 20 '22 20:10

Gabriele Mariotti


For those that struggle with this and the fact that their timestamp is off a day, here is my working solution. I have a requirement of API 23 so I could not use any of the nice Epoch functions in java.time.*. The key for me was realizing I need to do the timezone offset math.

        picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
        @Override
        public void onPositiveButtonClick(Long selectedDate) {
            // user has selected a date
            // format the date and set the text of the input box to be the selected date
            // right now this format is hard-coded, this will change
            ;
            // Get the offset from our timezone and UTC.
            TimeZone timeZoneUTC = TimeZone.getDefault();
            // It will be negative, so that's the -1
            int offsetFromUTC = timeZoneUTC.getOffset(new Date().getTime()) * -1;

            // Create a date format, then a date object with our offset
            SimpleDateFormat simpleFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
            Date date = new Date(selectedDate + offsetFromUTC);

            dataEntry.setText(simpleFormat.format(lDate));
        }
    });
    picker.show(myActivity.getSupportFragmentManager(), picker.toString());
like image 6
GR Envoy Avatar answered Oct 20 '22 20:10

GR Envoy