3

How to create alert dialog that I can select date of birth:

I want that it's not be impossible to choose date in future.

How could set limits in date dialog Here is my code

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                final Calendar calendar = Calendar.getInstance();
                int yy = calendar.get(Calendar.YEAR);
                int mm = calendar.get(Calendar.MONTH);
                int dd = calendar.get(Calendar.DATE);
                return new DatePickerDialog(this, mDatePickerListener, yy, mm, dd);
        }
        return null;
    }
pmb
  • 2,327
  • 3
  • 30
  • 47

3 Answers3

3

Just add this

dialog.getDatePicker().setMaxDate(new Date().getTime());
user1163234
  • 2,407
  • 6
  • 35
  • 63
1

I would recommend to use a pretty cool Material Design date picker library which is backwards compatible.

All you need to do is add the following line to your gradle dependecies list:

compile 'com.wdullaer:materialdatetimepicker:1.5.2' // The latest at this point.

And then you just simply use the setMinDate() and setYearRange() methods of the DatePickerDialog.

Something like this:

Calendar calendar = Calendar.getInstance();
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePickerDialog datePickerDialog, int i, int i1, int i2) {
            // Do whatever you want when the date is selected.
        }
}, calendar.get(Calendar.YEAR), 
   calendar.get(Calendar.MONTH),
   calendar.get(Calendar.DAY_OF_MONTH));
   datePickerDialog.setMinDate(calendar);
   datePickerDialog.setYearRange(calendar.get(Calendar.YEAR), 
               calendar.get(Calendar.YEAR) + YEARS_IN_THE_FUTURE); // You can add your value for YEARS_IN_THE_FUTURE.

Does this help?

Mike
  • 4,550
  • 4
  • 33
  • 47
  • 1
    Helped for me. Just add this to show it in a fragment: `datePickerDialog.show(getActivity().getFragmentManager(), "DatePickerDialog");` – Mario Velasco Oct 13 '15 at 10:01
0

If you are using kotlin in your code then this would help you

Note:: textView hold the reference of text where you want to set the date

private fun openDobPicker(textView: TextView) {
        val c = Calendar.getInstance()
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH)
        val day = c.get(Calendar.DAY_OF_MONTH)

        val dpd = DatePickerDialog(requireActivity(), DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
            val date = "${dayOfMonth}/${monthOfYear}/${year}"
            textView.text = date
        }, year, month, day)

        //restrict calendar to show future dates
        dpd.datePicker.maxDate = Date().time
        
        //If you want to limit the minimum date then do this
        //dpd.datePicker.minDate = PASSED_MIN_DATE_HERE
    

        
        dpd.show()
    }
Suraj Bahadur
  • 3,730
  • 3
  • 27
  • 55