Reputation: 3016
I want to select past date to max current date. There is methods like setMinDate() and setMaxDate() in single date selection in date picker. But, how i can do that in date range selection in material date range picker?
Upvotes: 2
Views: 1147
Reputation: 3954
You have to use CalendarConstraints for Minimum and Maximum dates restriction in Date Range picker.
val constraintsBuilderRange = CalendarConstraints.Builder()
val dateValidatorMin: DateValidator = DateValidatorPointForward.from(millisOfMinimum)
val dateValidatorMax: DateValidator = DateValidatorPointBackward.before(millisOfMaximum)
val listValidators = ArrayList<DateValidator>()
listValidators.add(dateValidatorMin)
listValidators.add(dateValidatorMax)
val validators = CompositeDateValidator.allOf(listValidators)
constraintsBuilderRange.setValidator(validators)
val datePicker = MaterialDatePicker.Builder.dateRangePicker()
.setTitleText("Select range")
.setCalendarConstraints(constraintsBuilderRange.build())
.build()
Upvotes: 4
Reputation: 3185
Just add these two lines of code in your code and you can prevent future date selection.
maxDate: moment().subtract(0, 'days'), "This Month": [moment().startOf("month"), moment().subtract(0, 'days')],
var rangesVal = {
position: "inherit",
drops: "down",
maxDate: moment().subtract(0, 'days'),
minDate: minDate,
opens: "center",
maxSpan: {
months: 12,
},
ranges: {
Today: [moment(), moment()],
Yesterday: [moment().subtract(1, "days"), moment().subtract(1, "days")],
"Last 7 Days": [moment().subtract(6, "days"), moment()],
"Last 30 Days": [moment().subtract(29, "days"), moment()],
"This Month": [moment().startOf("month"), moment().subtract(0, 'days')],
"Last Month": [
moment().subtract(1, "month").startOf("month"),
moment().subtract(1, "month").endOf("month"),
],
},
};
Upvotes: 0
Reputation: 174
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showRangePickerDialog()
}
private fun showRangePickerDialog() {
val builderRange = MaterialDatePicker.Builder.dateRangePicker()
builderRange.setCalendarConstraints(defineRange().build())
val pickerRange = builderRange.build()
pickerRange.show(supportFragmentManager, pickerRange.toString())
}
private fun defineRange(): CalendarConstraints.Builder {
val constraintsBuilderRange = CalendarConstraints.Builder()
val calendarStart: Calendar = GregorianCalendar.getInstance()
val calendarEnd: Calendar = GregorianCalendar.getInstance()
val year = 2020
calendarStart.set(year, 1, 1)
calendarEnd.set(year, 6, 30)
val minDate = calendarStart.timeInMillis
val maxDate = calendarEnd.timeInMillis
constraintsBuilderRange.setStart(minDate)
constraintsBuilderRange.setEnd(maxDate)
constraintsBuilderRange.setValidator(RangeValidator(minDate, maxDate))
return constraintsBuilderRange
}
}
class RangeValidator(private val minDate:Long, private val maxDate:Long) : CalendarConstraints.DateValidator{
constructor(parcel: Parcel) : this(
parcel.readLong(),
parcel.readLong()
)
override fun writeToParcel(dest: Parcel?, flags: Int) {
}
override fun describeContents(): Int {
}
override fun isValid(date: Long): Boolean {
return !(minDate > date || maxDate < date)
}
companion object CREATOR : Parcelable.Creator<RangeValidator> {
override fun createFromParcel(parcel: Parcel): RangeValidator {
return RangeValidator(parcel)
}
override fun newArray(size: Int): Array<RangeValidator?> {
return arrayOfNulls(size)
}
}
}
Try the above code but have to do some changes in range as per your requirement.
Upvotes: 0
Reputation: 153
Set your current date in setMaxDate()
And setMinDate() whatever you want to show.
Upvotes: 1