Does anyone know if it's possible in an Android DatePicker, to:
a) Let a user ONLY select a given block of dates. e.g.. user taps on a "W" day, then we automatically highlight that weeks Sunday -> Friday date range (each day is selected)
b) In the header of the DatePicker, show the selected date range. eg. Sun, Jan 14 - Fri, Jan 19
Really appreciate anyone's help/advice on this.
For future readers!
Actually with new android material design components what you want achieve could be achieved using MaterialDatePicker
. It's a dialog, not a custom view like android.widget.DatePicker
.
Steps
1. Add material dependency to your module's gradle file
implementation 'com.google.android.material:material:1.1.0-beta01'
2. Change app theme to inherit from a version of material theme.
ex:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
3. Use following code to initiate the dialog.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupRangePickerDialog()
}
private fun setupRangePickerDialog() {
val builderRange = MaterialDatePicker.Builder.dateRangePicker()
builderRange.setCalendarConstraints(limitRange().build())
val pickerRange = builderRange.build()
pickerRange.show(supportFragmentManager, pickerRange.toString())
}
/*
Limit selectable range to Oct 17 - Nov 20 2019
*/
private fun limitRange(): CalendarConstraints.Builder {
val constraintsBuilderRange = CalendarConstraints.Builder()
val calendarStart: Calendar = Calendar.getInstance()
val calendarEnd: Calendar = Calendar.getInstance()
val year = 2019
val startMonth = 10
val startDate = 17
val endMonth = 11
val endDate = 20
calendarStart.set(year, startMonth - 1, startDate - 1)
calendarEnd.set(year, endMonth - 1, endDate)
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) {
TODO("not implemented")
}
override fun describeContents(): Int {
TODO("not implemented")
}
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)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With