I am using date picker in my application couples of time. I want my date picker to show dates following some restrictions. I am using the below code:-
@Override
protected Dialog onCreateDialog(int id) {
switch(id) {
case DATE_PICKER_DIALOG:
datePickerDialogDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {
year = selectedYear;
month = selectedMonth;
date = selectedDate;
activitySignUpDateEditText.setText(new StringBuilder().append(date));
activitySignUpMonthEditText.setText(new StringBuilder().append(month + 1));
activitySignUpYearEditText.setText(new StringBuilder().append(year));
}
};
return new DatePickerDialog(this, datePickerDialogDateSetListener, year, month, date);
default:
return null;
}
}
Somewhere i want the date picker to show maximum date 18 years back from current date & somewhere minimum date as the current date. I have searched a lot but doesn't found any satisfactory answer. I know about setMaxDate() & setMinDate() methods of date picker but when i tried with it, i am unable to set selected date on my edit text. any help would be appreciable. Thanks in advance.
You can try replacing this line:
return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
By those:
DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();
Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMaxDate(calendar.getTimeInMillis());//set the current day as the max date
return dpDialog;
in the same way you set the minimum date as well
DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();
Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMinDate(calendar.getTimeInMillis());//set the current day as the max date
return dpDialog;