0

I'm working on a form to input user info. There's an EditText to set the date of birth. tapping on it opens a DatePickerDialogue. I want to set limit on the date to current date so that user cannot select date from the future.

Here's my Code:

DatePickerDialog.OnDateSetListener dpd;
Edittext etDOB = (EditText) findViewById(R.id.etDate);
etDOB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(ActivityCustomerInfo.this,dpd,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
dpd=new DatePickerDialog.OnDateSetListener(){
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, monthOfYear);
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String month = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
                etDOB.setText(new StringBuilder().append((month)+" ").append((dayOfMonth)+", ").append(year));
            }
        };

I know a function setMaxDate() but cannot understand how to apply it to my code.

I followed the answers here but the code doesn't seem to match with mine.

Community
  • 1
  • 1
Arsal
  • 565
  • 2
  • 8
  • 17
  • Take a look at this answer, http://stackoverflow.com/a/16749501/5225620 just create a reference to your DatePickerDialog so you can call the getDatePicker() method, then use the setmaxDate(). – Rafael Jul 12 '16 at 12:35
  • Can you please elaborate? I'm sorry to sound dumb but I'm beginner to android. @Rafael – Arsal Jul 12 '16 at 12:46
  • 1
    Where you're creating your new DatePickerDialog inside your onClick method, try this `DatePickerDialog dialog = new DatePickerDialog(ActivityCustomerInfo.this,dpd,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); dialog.getDatePicker().setMaxDate(new Date().getTime()); dialog.show();` – Rafael Jul 12 '16 at 12:59
  • If you still having trouble here is the code more clean: https://gist.github.com/faelmorais/d507cc655487843fda36124c48b641d5 – Rafael Jul 12 '16 at 13:07
  • thank you for making it easier to understand @Rafael. solved the problem – Arsal Jul 12 '16 at 13:27

0 Answers0