0

I use Calendar on EditText click to select birth date of user and set it in EditText. It works fine in all devices. But only in Swipe Sonic 4.1.2 (API 16) it shows years up to 1980. User can not select year lower than 1980.

Here is my Code:

 demo.setOnFocusChangeListener(new View.OnFocusChangeListener() {

public void onFocusChange(View v, boolean hasFocus) {
 if (hasFocus) {
DateDialog();
 }
}
});



public void DateDialog() {

        try {
            DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                    demo.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
                    edt_contact.requestFocus();
                    // checking for age should be  18 years and above
                    Calendar userAge = new GregorianCalendar(year, monthOfYear, dayOfMonth);
                    Calendar minAdultAge = new GregorianCalendar();
                    minAdultAge.add(Calendar.YEAR, -18);
                    if (minAdultAge.before(userAge)) {
                        Toast.makeText(getApplicationContext(), "Age should be 18 and above", Toast.LENGTH_LONG).show();

                        demo.setText("");
                        // demo.requestFocus();
                    }

                }
            };

            DatePickerDialog dpDialog = new DatePickerDialog(this, listener, year, month, day);
            dpDialog.setCancelable(true);
            dpDialog.show();

        } catch (Exception e)

        {

        e.printStackTrace();
        logsdata.logsdata("MainActivty", "DateDialog", e.getMessage().toString());
    }

}//DateDialog

1 Answers1

0

If you target API >= 11, you can try setting the minimum date, like this:

DatePickerDialog dpDialog = new DatePickerDialog(this, listener, year, month, day);
dpDialog.setCancelable(true);
dpDialog.show();
dpDialog.getDatePicker().setMinDate(0); //This will set the minimum date to 01/January/1970
Marco Batista
  • 1,410
  • 1
  • 20
  • 38
  • Yes it works. But i want min year to 1900.Because I want to use Birth date. – pavan mahajan Aug 08 '15 at 06:48
  • I didn't test this, but you may be able to set the `minDate` to `-2208988800`, try it and let me know if it works :) – Marco Batista Aug 08 '15 at 06:51
  • According to the docs, it should work.. maybe is a device specific issue.. anyway you can try these solutions http://stackoverflow.com/questions/8620781/datepickerdialog-incorrectly-enforcing-a-minimum-date-of-jan-1-1970 – Marco Batista Aug 08 '15 at 07:12
  • I download one app from play store. That work fine in same device. ok I need to do more work on it. – pavan mahajan Aug 08 '15 at 07:24
  • I got 2 solutions 1).fromDatePickerDialog.getDatePicker().setMinDate(-1576800000000L); this sets upto 1920 2).newCalendar.set(1888, Calendar.JANUARY, 1, 0, 0, 0); fromDatePickerDialog.getDatePicker().setMinDate(newCalendar.getTimeInMillis()); this sets yesr upto 1888 – pavan mahajan Aug 08 '15 at 08:04