0

This is code to pop up when clicking EditText field with a date?

final DatePickerDialog.OnDateSetListener lastdateof = new DatePickerDialog.OnDateSetListener()  {

        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

            String format = "dd MMMM yy";
            calendar.set(calendar.YEAR, year);
            calendar.set(calendar.MONTH, monthOfYear);
            calendar.set(calendar.DAY_OF_MONTH, dayOfMonth);

            datePicker.setMinDate(System.currentTimeMillis()-1000);
            SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
            lastdate.setText(sdf.format(calendar.getTime()));
        }
    };


    lastdate.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void onClick(View view) {
          new DatePickerDialog(AddActivity.this, lastdateof, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();

        }
    });

how to disable or make not selectable of data picker date before current date?

Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66
Marzook
  • 371
  • 1
  • 5
  • 20

2 Answers2

2

try this, setMinDate before showing dialog

    Calendar myCalendar = Calendar.getInstance();

    lastdate.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    //Datepiker dialog
                    DatePickerDialog datePickerDialog = new DatePickerDialog(AddEditDiaryActivity.this, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH));
                    datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
                    datePickerDialog.show();
                }
            });

   DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            // TODO Auto-generated method stub
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            String myFormat = "yyyy-MM-dd"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.ENGLISH);

        lastdate.setText(sdf.format(myCalendar.getTime()));
        }

    };
gaurang
  • 2,217
  • 2
  • 22
  • 44
0

You have to set min date before showing the date picker.

datePicker.setMinDate(System.currentTimeMillis()-1000);

Call the above code before call show() of your date picker.

Eg:

datePicker.setMinDate(System.currentTimeMillis()-1000);
datePicker.show();
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66