Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Disable future dates in Android date picker

How to Disable future dates in Android date picker

Java Code :

mExpireDate.setOnClickListener(new OnClickListener() {              @Override             public void onClick(View v) {                  // To show current date in the datepicker                 final Calendar mcurrentDate = Calendar.getInstance();                 int mYear = mcurrentDate.get(Calendar.YEAR);                 int mMonth = mcurrentDate.get(Calendar.MONTH);                 int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);                   DatePickerDialog mDatePicker = new DatePickerDialog(                         EventRegisterActivity.this, new OnDateSetListener() {                             public void onDateSet(DatePicker datepicker,                                     int selectedyear, int selectedmonth,                                     int selectedday) {                                  mcurrentDate.set(Calendar.YEAR, selectedyear);                                 mcurrentDate.set(Calendar.MONTH, selectedmonth);                                 mcurrentDate.set(Calendar.DAY_OF_MONTH,                                         selectedday);                                 SimpleDateFormat sdf = new SimpleDateFormat(                                         getResources().getString(                                                 R.string.date_card_formate),                                         Locale.US);                                  mExpireDate.setText(sdf.format(mcurrentDate                                         .getTime()));                             }                         }, mYear, mMonth, mDay);                  mDatePicker.setTitle(getResources().getString(                         R.string.alert_date_select));                 mDatePicker.show();             }         }); 

How to do it?

like image 486
venu Avatar asked Jan 07 '14 11:01

venu


People also ask

How do you turn off future date in date range picker?

// date range picker $('. date_input'). daterangepicker({ autoUpdateInput: false, locale: { cancelLabel: 'Clear', format: 'DD-MM-YY' } });

How do I turn off past date on android?

Using the setMinDate(…) method on the DatePicker class to grey out and disable the selection of dates that occured before the time value passed as a parameter.

How do you turn off the future date in flutter?

In the Flutter date range picker, you can enable or disable the past dates by using the enablePastDates property of the calendar. When setting the enablePastDates property value as false, you can't able to select the past dates.

How do I show only future dates in DatePicker?

Here is a complete solution where you can use <apex:input type="date" with jQuery Datepicker where property minDate set to 0 (to show only future dates). I used a normal html <input type="text" to show the jQuery Datepicker and hide the <apex:input type="date" field.


2 Answers

Get the DatePicker from DatePickerDialog with getDatePicker(). Set the max date to current date with setMaxDate():

mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis()); 

Requires API level 11.

like image 194
laalto Avatar answered Oct 14 '22 15:10

laalto


You can call getDatePicker().setMaxDate(long) on your DatePickerDialog to set today as your maximum date. You can update the function with the same name from the snippet you posted.

Note:: DatePickerDialog is the object that I referenced in the Android Docs from the link I posted.

@Override protected Dialog onCreateDialog(int id) {     Calendar c = Calendar.getInstance();     int cyear = c.get(Calendar.YEAR);     int cmonth = c.get(Calendar.MONTH);     int cday = c.get(Calendar.DAY_OF_MONTH);     switch (id) {         case DATE_DIALOG_ID:         //start changes...         DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);         dialog.getDatePicker().setMaxDate(System.currentTimeMillis());         return dialog;         //end changes...     }     return null; } 

Try this and give your feedback!!!

like image 27
Satyaki Mukherjee Avatar answered Oct 14 '22 15:10

Satyaki Mukherjee