How can I set the specific selected date to min date in android date picker? (java)

474 Views Asked by At

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 set the Data which user selected in Calander startDate and set this data as MinDate at end date. I've tried in this way but it's not working :(

I can't use Mateiral Range Date Picker because I have to use specific theme. Also I'm not good at programming so I'd appreciate it if you could explain it in detail.

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();
    }
});
1

There are 1 best solutions below

3
On

This code:

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

Should be inside

ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
}

Because startYear, startMonth and startDay are only set on ipDcEventStartDay.setOnClickListener(), so at the beginning they have some value but not the one that was set on ipDcEventStartDay.setOnClickListener()

So in ipDcEventEndDay.setOnClickListener() you have to set again on startDate the values that were picked on the other DatePickerDialog