Angela Heely
Angela Heely

Reputation: 609

Is it possible to switch the format of date picker header?

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? enter image description here

enter image description here

material designs : https://material.io/components/date-pickers

what I want to achieve is this : enter image description here

Upvotes: 1

Views: 1598

Answers (2)

luk321
luk321

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

minchaej
minchaej

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:

  • Change date format: setLocale(Locale locale)
  • Choose from multiple date UIs ✅
  • Very flexiable and easily customizlbe to fit your needs ✅

PS: There is one more popular alternative: android-betterpickers

Let me know if you have any other questions.

Upvotes: 1

Related Questions