Reputation: 331
I want to disable my past dates in DatePicker
and show only current and future dates... I am using the code below but this will display past dates also, please suggest the changes needed.
date_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
mDay = calendar.get(Calendar.DAY_OF_MONTH);
mMonth = calendar.get(Calendar.MONTH);
mYear = calendar.get(Calendar.YEAR);
OnDateSetListener callBack = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Calendar calender_check = Calendar.getInstance(Locale.getDefault());
calender_check.set(Calendar.YEAR, year);
calender_check.set(Calendar.MONTH, monthOfYear);
calender_check.set(Calendar.DAY_OF_MONTH, dayOfMonth);
SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy");
Date date2 =calender_check.getTime();
updatedate(sdf2.format(date2),originalFormat.format(date2));
Log.d("Date >>>>", ""+date2);
Log.d("sel_date", sdf2.format(date2));
date_btn.setText(sdf2.format(date2));
}
};
DatePickerDialog dialog = new DatePickerDialog(Class.this, callBack, mYear, mMonth, mDay);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == DialogInterface.BUTTON_NEGATIVE) {
// Log.i(TAG, "Cancel");
//isCancelled = true;
dialog.dismiss();}
}
});
dialog.setCancelable(false);
dialog.show();
}
});
Upvotes: 0
Views: 6744
Reputation: 802
before creating your DatePickerDialog,set the minimum date and then show it.
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActiviy.this,date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
SimpleDateFormat simpledateformate = new SimpleDateFormat("dd/MM/yyyy");
Date date = simpledateformate.parse("21/12/2012");
datePickerDialog.getDatePicker().setMinDate(d.getTime());
datePickerDialog.show();
Upvotes: 2
Reputation: 2445
You can probably try a code like that:
DatePicker view;
final long time = System.currentTimeMillis() - 1;
//Set min time to now
view.setMinDate(time);
Upvotes: 2