lye yan nian lye yan nian - 1 year ago 90
Java Question

Android date picker as no backdate can be selected, only today and future date

i do have one date picker as i just want to make it only can select today's date and future date, no back date. Need some guidelines on how it can be done, maybe spinner can do that?

//start date
TableRow r8 = new TableRow(this);
TextView tvStartDate = new TextView(this);
tvStartDate.setText("Start Date: ");
final EditText etStartDate = new EditText(this);
etStartDate.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Calendar dateNow = Calendar.getInstance();
int year = dateNow.get(Calendar.YEAR);
int month = dateNow.get(Calendar.MONTH);
int day = dateNow.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dp;
dp = new DatePickerDialog(AddPetActivity.this, new OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
selectedmonth = selectedmonth + 1;
etStartDate.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
}, year, month, day);
dp.setTitle("Select Date");;

Answer Source

You can access the dialog's date picker and set it's min/max dates like so:


Note that when setting a minimum date for a picker it must be before the selected date of the picker, otherwise you get an IllegalArgumentException (same goes for max date of course).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download