Shamsi/Jalali date picker with material design (Google Material Components Date Picker)
This library is based on Googles Material Date Picker. I reimplemented everything in Kotlin.
Minimum SDK: 21
Add this to your gradle file
implementation 'com.xdev.arch.persiancalendar.datepicker:datepicker:0.3.2'
The picker can be customized via the MaterialDatePicker.Builder and the CalendarConstraints.Builder. These classes allow you to
- Select the mode: single date or range of dates.
- Select the bounds: bounds can be restricted to any contiguous set of months. Defaults Farvardin, 1388. to Esfand, 1409.
- Select valid days: valid days can restrict selections to weekdays only. Defaults to all days as valid.
- Set a title.
- Set the month to which the picker opens (defaults to the current month if within the bounds otherwise the earliest month within the bounds).
- Set a default selection (defaults to no selection).
First you can define a CalendarConstraints to limit dates You can use PersianCalendar class to set a Persian/Shamsi date or a java.util.Calendar instance
val calendar = PersianCalendar()
calendar.setPersian(1340, Month.FARVARDIN, 1)
val start = calendar.timeInMillis
calendar.setPersian(1409, Month.ESFAND, 29)
val end = calendar.timeInMillis
val openAt = PersianCalendar.getToday().timeInMillis
val constraints = CalendarConstraints.Builder()
.setStart(start)
.setEnd(end)
.setOpenAt(openAt)
.setValidator(DateValidatorPointForward.from(start)).build()
Then we should create MaterialDatePicker
val datePicker = MaterialDatePicker.Builder
.datePicker()
.setTitleText("تاریخ را انتخاب کنید.")
.setCalendarConstraints(constraints).build()
val rangePicker = MaterialDatePicker.Builder
.dateRangePicker()
.setTitleText("محدوده را انتخاب کنید.")
.setCalendarConstraints(constraints).build()
And finaly call show()
datepicker.show(supportFragmentManager, "aTag")
rangePicker.show(supportFragmentManager, "aTag")
There are 4 listeners:
- OnPositiveButtonClickListener
- OnNegetiveButtonClickListener
- OnCancelListener
- OnDismissListener
Refer to Sample's styles.xml
Rahman Mohammadi @axdeveloper
2020 Rahman Mohammadi (@axdeveloper). See the LICENSE
file.