0

I'm a student and I'm working on an android reservation app.In these app I have a button that choose date, when I click on the button a dialogue box appear for choosing the date, but I want to disable all the previous dates and just want to show current date and further 3 dates of a month.kindly help me how can I do this Thanks

pPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });
    final Calendar cal = Calendar.getInstance();
    pYear = cal.get(Calendar.YEAR);
    pMonth = cal.get(Calendar.MONTH);
    pDay = cal.get(Calendar.DAY_OF_MONTH);

    /** Display the current date in the TextView */
    updateDisplay();

}
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                    pDateSetListener,
                    pYear, pMonth, pDay);
    }
    return null;
}

private void updateDisplay() {
    pDisplayDate.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(pMonth + 1).append("/")
                    .append(pDay).append("/")
                    .append(pYear).append(" "));

}
Ahlem Jarrar
  • 1,129
  • 1
  • 12
  • 33

2 Answers2

0

You can set a min and max date for DatePicker, do something like this:

yourDatePickerDialog.getDatePicker().setMinDate(youMinDate);

https://developer.android.com/reference/android/widget/DatePicker.html#setMinDate(long)

pawelo
  • 1,405
  • 4
  • 15
  • 30
0

try this one

DatePickerDialog datePicker;
 private Calendar calendar;
private int year, month, day;
      // FETCH YEAR,MONTH,DAY FROM CALENDAR
   calendar = Calendar.getInstance();
    year = calendar.get(Calendar.YEAR);
    month = calendar.get(Calendar.MONTH);
    day = calendar.get(Calendar.DAY_OF_MONTH);

datePicker = new DatePickerDialog(this, YOUR_LISTENER, year, month, day);
// this line is used for disable previous date but u can select the date
datePicker.getDatePicker().setMinDate(System.currentTimeMillis());
// this line is used to prevent date selection 
 datePicker.getDatePicker().setCalendarViewShown(false);
anu
  • 213
  • 1
  • 3
  • 10