Android DatePicker : Disabling the PREVIOUS dates based on the CURRENT date

*爱你&永不变心* 提交于 2019-11-29 15:34:24
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    // the callback received when the user "sets" the Date in the
    // DatePickerDialog
    public void onDateSet(DatePicker view, int yearSelected,
            int monthOfYear, int dayOfMonth) {
        year = yearSelected;
        month = monthOfYear + 1;
        day = dayOfMonth;
        // Set the Selected Date in Select date Button
        txtarrivedate.setText(year + "-" + month + "-" + day);
    }
};

// Method automatically gets Called when you call showDialog() method

   @Override
   protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 1:
        // create a new DatePickerDialog with values you want to show

        DatePickerDialog da = new DatePickerDialog(this, mDateSetListener,
                mYear, mMonth, mDay);
        Calendar c = Calendar.getInstance();

        c.add(Calendar.DATE, 1);
        Date newDate = c.getTime();
        da.getDatePicker().setMinDate(newDate.getTime());
        return da;
        // create a new TimePickerDialog with values you want to show
    case 2:
        // create a new DatePickerDialog with values you want to show
        DatePickerDialog da1 = new DatePickerDialog(this,
                mDateSetListener2, mYear, mMonth, mDay);
        Calendar c1 = Calendar.getInstance();

        c1.add(Calendar.DATE, 1);
        Date newDate2 = c1.getTime();
        da1.getDatePicker().setMinDate(newDate2.getTime());
        return da1;

    }
    return null;
}

only use use the for calling dialog showDialog(1);

Try this:

// set date picker with current date
DatePickerDialog date = new DatePickerDialog(this, datePickerListener, mYear,mMonth,mDay){
            // Comapre the date selected in picker with the current date.      
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
            {   
                if (year < mYear)
                    view.updateDate(mYear,mMonth,mDay);

                if (monthOfYear < mMonth && year == mYear)
                    view.updateDate(mYear,mMonth,mDay);

                if (dayOfMonth < mDay && year == mYear && monthOfYear == mMonth)
                    view.updateDate(mYear,mMonth,mDay);

            }
        };
        return date;
    }        

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

    // as soon as the dialog box is closed, this method will be called.
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        mYear = selectedYear;
        mMonth = selectedMonth;
        mDay = selectedDay;

        // set selected date into textview
        txtDate.setText(new StringBuilder().append(mMonth + 1)
                .append("-").append(mDay).append("-").append(mYear)
                .append(" "));    

    }
};

Simply try this solution:

dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

I don't think you can actually disable the date picker. But once the date is selected, you can validate it with current date and make the user reselect future date. You can send a toast message if the user selects the past date.

In your onDateSet you can validation on date like you want to set date only after today's date then add the following code in onDateSet method

            Calendar cl = Calendar.getInstance();
            cl.set(year, monthOfYear, dayOfMonth);
            if (cl.after(Calendar.getInstance())) { 
                    //set your date
            }else{
                    //do nothing
            }

hope this will work for you

etDateOfDelivery.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new DatePickerDialog(SendOrderActivity.this,
                    new OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker arg0, int year,
                                int month, int day) {

                            if(month==intMonthOfYear && day<intDayOfMonth &&year<=intYear)
                            {
                                    // your message. dont use dialogmessage. its my custom dialog. use in your away
                                DialogMessage.customDialogSingleButton(context, "Invalid Date", "Please Set Comming Date", "Cancel");
                            }
                            else if(month<=intMonthOfYear && day==intDayOfMonth && year<=intYear)
                            {
                                DialogMessage.customDialogSingleButton(context, "Invalid Date", "Please Set Comming Date", "Cancel");
                            }
                            else if(month<=intMonthOfYear && day<=intDayOfMonth && year<intYear)
                            {
                                DialogMessage.customDialogSingleButton(context, "Invalid Date", "Please Set Comming Date", "Cancel");
                            }
                            else
                            {


                                if(month<9)
                                {
                                    etDateOfDelivery.setText("0"+(month + 1) + "/"
                                            + day+ "/" + year);
                                }
                                else
                                {
                                    etDateOfDelivery.setText((month + 1) + "/"
                                            + day+ "/" + year);
                                }

                            }

                        }
                    }, intYear, intMonthOfYear, intDayOfMonth).show();
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!