Reputation: 609
I am trying to change the header on material design date picker as you select a date to a different format than default. Current default is Month , year : ex: june 22, 2021, but I want it such that its Mon, June 22. It's shown on material design document as the first design, but there's no indication of how to go about changing this format? Any ideas?
material designs : https://material.io/components/date-pickers
what I want to achieve is this :
Upvotes: 1
Views: 1598
Reputation: 116
After looking into the source, and trying some things, I did find a solution. Unfortunately, it's not that simple to implement and I guess switching to MaterialDateTimePicker
is still the best solution here.
The conversion from date to text for the header is done by the DateSelector
via getSelectionDisplayString(Context context)
. See getHeaderText()
inside of MaterialDatePicker
here.
Sadly, MaterialDatePicker
is final, so you can not simply create your own and override this method. But, with the given Builder, you can use MaterialDatePicker.Builder.customDatePicker(DateSelector selector)
to place your own DateSelector (source).
Material uses their SingleDateSelector
(source) for a simple date selection. But again, it's not possible to build upon this selector and write a custom getSelectionDisplayString
method, because the class is restricted. Knowing this, it's needed to write your own DateSelector
.
Building your own DateSelector
is a bigger task on its own. It may be possible to copy from SingleDateSelector
, but it's using some private classes and methods you need to copy, too.
To cut a long story short, I guess this is too much for "simply" changing the date format.
Upvotes: 0
Reputation: 1814
Unfortunately, you can NOT change Date Format
and Local
of MaterialDatePicker
.
Since MaterialDatePicker
is very restricting, so as many other Material Design Components
, I recommend you to use an alternate Date Picker Library such as MaterialDateTimePicker.
It basically fulfills all your needs:
setLocale(Locale locale)
✅PS: There is one more popular alternative: android-betterpickers
Let me know if you have any other questions.
Upvotes: 1