I found DatePicker(Dialog) very confusing... When setMinDate() is set... the shown calendar really "visually disable" passed dates (make them gray) but these dates are still posible to select! I want to user prevent to do so. Ideal way would be make passed dates non-clickable / unselectable. If it is not possible, than at least catch this event (which one? onDateChanged?, onSelected? ...) and toast a message and disable OK button until user selects a valid date.
Unfortunatelly, datepicker(dialog) has no onChangedListener (actually it has but has no setter for it). So the only event I get is on OK button pressed. It is too late.
Here is my code
dateTextView = (TextView) view.findViewById(R.id.createOrder_date);
dateTextView.setText(R.string.createOrder_time_now);
dateTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar calendarNow = Calendar.getInstance();
final int yearNow = calendarNow.get(Calendar.YEAR);
final int monthNow = calendarNow.get(Calendar.MONTH);
final int dayNow = calendarNow.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(CreateOrderFragment.this.getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
if (calendar.before(calendarNow)) {
Toast.makeText(CreateOrderFragment.this.getActivity(), getResources().getString(R.string.createOrder_datePicker_selectFutureDate), Toast.LENGTH_SHORT).show();
view.updateDate(yearNow, monthNow, dayNow);
calendar = calendarNow;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
dateTextView.setText(sdf.format(calendar.getTime()));
}
}, yearNow, monthNow, dayNow);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.setTitle(getResources().getString(R.string.createOrder_datePicker_title));
datePickerDialog.show();
}
});
Setting a Minimum Selectable Date in the Android DatePicker By setting a minimum selectable date on your DatePicker using the setMinDate(…) method, the dates before that will become greyed out and not selectable in the Calendar.
Now override a method onCreateDialog and instead of returning super. onCreateDialog return an instance of DatePickerDialog. Now pass parameters to the constructor of DatePickerDialog which requires context, OnDateSetListener, year, month, dayOfMonth.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Kindly fine the highlighted code, this is the simplest way to change the colour of your datePicker.
How to set the date in datepicker dialog in android? This example demonstrates about how do I set the date in datepicker dialog in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project
How to disable future dates in JavaScript Datepicker? In order to disable future dates, you need to use maxDate and set the current date. Following is the JavaScript code − In order to run the above program, I have saved this file with the name index.html.
To disable a date in the DateRangePicker, use either of the following approaches: To disable dates by setting an array, list the names of the days that will be disabled by using the first letters from their names in English. To disable dates by using a function, set the return value for the date that will be disabled to true.
Following is the JavaScript code − In order to run the above program, I have saved this file with the name index.html. Right click on this file and select option open with live server. After that, click the mouse on text box, that will display the date picker as in the below screenshot −
When you receive the date in onDateSent(), just check if it's earlier than the date you set in setMinDate(). If so, then just show the DatePickerDialog again.
Code:
final long tomorrow = System.currentTimeMillis() + DateUtils.MILLIS_IN_DAY;
private void showDatePickerDialog()
{
mDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
//kv If user tries to select date in past (or today)
if (calendar.getTimeInMillis() < tomorrow)
{
//kv Make them try again
showDatePickerDialog();
Toast.makeText(this, "Invalid date, please try again", Toast.LENGTH_LONG).show();
}
else
{
//success
}
}
}, calendarTwoWeeksInFuture.get(Calendar.YEAR), calendarTwoWeeksInFuture.get(Calendar.MONTH), calendarTwoWeeksInFuture.get(Calendar.DAY_OF_MONTH));
//kv Set minimum date as tomorrow
mDatePickerDialog.getDatePicker().setMinDate(tomorrow);
mDatePickerDialog.show();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With