How to set default display in year format in Android Dialog datepicker

I am trying to figure out how to set the default display year of display in Android Dialog DatePicker . In our scenario, the first user must select the year, then the month, and finally the date in the DatePicker dialog box. For this requirement, we need to open a year-wise display when opening a dialogue.

Link to PFB image. Please refer to the image on the right side, which should open by default.

Datepicker

+5
source share
1 answer

You can use the Calendar and subtract the years you want, for example:

 public Dialog onCreateDialog(Bundle saveInstanceState) { final Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR, -18); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); return new DatePickerDialog(getActivity(), this, year, month, day); } 
0
source

All Articles