0

I have 2 DatePickers, one is start date picker and the other is end date picker. I want to set the min date of the end DatePicker to the start date which user has selected.

I've tried in this way but it's not working :(

    ipDcEventStartDay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            final Calendar c = Calendar.getInstance();

            startYear = c.get(Calendar.YEAR);
            startMonth = c.get(Calendar.MONTH);
            startDay = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    startYear = year;
                    startMonth = month+1;
                    startDay = dayOfMonth;

                    ipDcEventStartDay.setText(startYear + " - " + startMonth + " - " + startDay);
                    ipDcEventStartDay.setTextColor(Color.parseColor("#000000"));
                }
            }, startYear, startMonth, startDay);
            dpd.show();
        }
    });

    Calendar startDate = Calendar.getInstance();
    startDate.set(Calendar.YEAR, startYear);
    startDate.set(Calendar.MONTH, startMonth);
    startDate.set(Calendar.DAY_OF_MONTH, startDay);

    ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Calendar c = Calendar.getInstance();

            endYear = c.get(Calendar.YEAR);
            endMonth = c.get(Calendar.MONTH);
            endDay = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    endYear = year;
                    endMonth = month;
                    endDay = dayOfMonth;

                    ipDcEventEndDay.setText(endYear + " - " + (endMonth+1) + " - " + endDay);
                    ipDcEventEndDay.setTextColor(Color.parseColor("#000000"));
                }
            }, endYear,endMonth, endDay);
            dpd.getDatePicker().setMinDate(startDate.getTimeInMillis());
            dpd.show();
        }
    });
  • You can try with the [MaterialDatePicker](https://github.com/material-components/material-components-android/blob/master/docs/components/DatePicker.md). It works with a range. – Gabriele Mariotti Apr 13 '21 at 12:32

1 Answers1

0

Note:: TextView hold the reference of text where you want to set the date

private fun openDobPicker(textView: TextView) {
            val c = Calendar.getInstance()
            val year = c.get(Calendar.YEAR)
            val month = c.get(Calendar.MONTH)
            val day = c.get(Calendar.DAY_OF_MONTH)
    
            val dpd = DatePickerDialog(requireActivity(), DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
                val date = "${dayOfMonth}/${monthOfYear}/${year}"
                textView.text = date
            }, year, month, day)
    
            //restrict calendar to show future dates
            dpd.datePicker.maxDate = Date().time
            
            //limit the minimum date then do this
            dpd.datePicker.minDate = PASSED_MIN_DATE_HERE
        
    
            
            dpd.show()
        }
Suraj Bahadur
  • 3,730
  • 3
  • 27
  • 55