Saty
Saty

Reputation: 2621

Restrict the year of the DatePicker dialogue to current year only

I am developing an android app where I want to restrict the date picker dialog' year to current year only that means,

User can not change the year to previous years or next years.

Could you please let me know know the way. I tried other answer however found that setMinDate() method however that is for dates only. if that can be used, how can it be?

Upvotes: 2

Views: 1215

Answers (2)

Oguz Ozcan
Oguz Ozcan

Reputation: 1714

Setting minumum and maximum date is not enough. You should also set the year range like this:

datePicker.setYearRange(calendar.get(Calendar.YEAR),calendar.get(Calendar.YEAR) + 1);

Otherwise user can click on the year and select any other year.

Upvotes: 0

Jitendra A
Jitendra A

Reputation: 1658

Why don't you set minimum date to 1 January and maximum date to 31 Dec of current year.

Calendar c = Calendar.getInstance();
 c.set(Calendar.MONTH,Calendar.JANUARY);
        c.set(Calendar.DAY_OF_MONTH,1);
dialog.getDatePicker().setMinDate(c.getTimeInMillis())

and similarly for setting Max date.

Upvotes: 2

Related Questions