1
Calendar calendar = Calendar.getInstance();
        MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();
        builder.setTitleText(R.string.title_pick_a_date);
        if (dateDF == null) {
            builder.setSelection(calendar.getInstance().getTimeInMillis());
        } else {
            String date = dateDF.toString();
            calendar.setTime(dateDF);
            builder.setSelection(calendar.getTimeInMillis());
        }
        MaterialDatePicker<Long> picker = builder.build();
        picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long> 
          () {
            @Override
            public void onPositiveButtonClick(Long selection) {
                calendar.setTimeInMillis(selection);
                calendar.setTimeZone(calendar.getTimeZone());
                int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
                int month = calendar.get(Calendar.MONTH);
                int year = calendar.get(Calendar.YEAR);
                String dateSF = dayOfMonth + "/" + (month + 1) + "/" + year;
                calendar.set(year, month, dayOfMonth);
                Date date = calendar.getTime();
                String a = "a";
                binding.layoutContentFeedFormulaDetails.tIeDate.setText(dateSF);
                binding.layoutContentFeedFormulaDetails.tIeDate.setTag(calendar.getTime());
            }
        });

        binding.layoutContentFeedFormulaDetails.btnDatePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                picker.show(getSupportFragmentManager(), picker.toString());
            }
        });

Hi,

I am using android MaterialDatePicker in an android application I am building.

The above code works perfectly fine. However, say for example, I have set the date to 26/08/2018 and open the materialDatePicker dialog, the calendar is scrolled to the current date and not the set date. I want the calendar scrolled automatically to the set date when the dialog is opened. I frantically looked all over the internet and couldn't find an answer. Please help me!

Thank you very much.

Sorry! This is my first question here on Stack overflow. I hope I didn't break any of its guidelines.

Jagadish Nallappa
  • 814
  • 1
  • 10
  • 11

1 Answers1

0

Rise and shine they say. This morning, I am glad to inform anyone who reads this that I found a solution to this. I hope this helps somebody.

  1. Create a constrainst builder.

CalendarConstraints.Builder calendarConstraintsBuilder = new CalendarConstraints.Builder();

  1. Get time in milliseconds from the calendar instance.

long openAt = calendar.getTimeInMillis();

  1. Set openAt to Calendar constraints builder with the time obtained in milliseconds.

calendarConstraintsBuilder.setOpenAt(openAt);

  1. Finally set calendarConstraintsBuilder.constraints to MaterialDatePicker.Builder instance.

builder.setCalendarConstraints(calendarConstraintsBuilder.build());

I know that by going to the definition of setOpenAt(long month) it again misleads people again into passing only the month as its parameter by looking at public Builder setOpenAt(long month). But reading the code documentation helps you know that it is time in milliseconds that you need to pass. The whole code is as follows.

MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();
        CalendarConstraints.Builder calendarConstraintsBuilder = new CalendarConstraints.Builder();
        builder.setTitleText(R.string.title_pick_a_date);
        if (dateDF == null) {
            builder.setSelection(Calendar.getInstance().getTimeInMillis());
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateDF);
            long openAt = calendar.getTimeInMillis();
            calendarConstraintsBuilder.setOpenAt(openAt);
            builder.setCalendarConstraints(calendarConstraintsBuilder.build());
            builder.setSelection(calendar.getTimeInMillis());
        }
        MaterialDatePicker<Long> picker = builder.build();
        picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
            @Override
            public void onPositiveButtonClick(Long selection) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(selection);
                calendar.setTimeZone(calendar.getTimeZone());
                int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
                int month = calendar.get(Calendar.MONTH);
                int year = calendar.get(Calendar.YEAR);
                String dateSF = dayOfMonth + "/" + (month + 1) + "/" + year;
                calendar.set(year, month, dayOfMonth);
                binding.layoutContentFeedFormulaDetails.tIeDate.setText(dateSF);
                binding.layoutContentFeedFormulaDetails.tIeDate.setTag(calendar.getTime());
            }
        });

        binding.layoutContentFeedFormulaDetails.btnDatePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                picker.show(getSupportFragmentManager(), picker.toString());
            }
        });
Jagadish Nallappa
  • 814
  • 1
  • 10
  • 11