Is it possible to change the datepicker (and also the timepicker) color scheme for Android 5.0?
I've tried setting the accent colors, but this doesn't work (both with and without android
):
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/purple</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/purple_tint</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">@color/purple_tint</item>
From original:
To something like this:
Best Answer
The reason why Neil's suggestion results in a fullscreen
DatePicker
is the choice of parent theme:Moreover, if you go this route, you have to specify the theme while creating the
DatePickerDialog
:This, in my opinion, is not good. One should try to keep the styling out of java and inside styles.xml/themes.xml.
I do agree that Neil's suggestion, with a bit of change (changing the parent theme to say,
Theme.Material.Light.Dialog
) will get you the desired result. But, here's the other way:On first inspection, we come across
datePickerStyle
which defines things such as:headerBackground
(what you are trying to change),dayOfWeekBackground
, and a few other text-colors and text-styles.Overriding this attribute in your app's theme will not work.
DatePickerDialog
uses a separate theme assignable by the attributedatePickerDialogTheme
. So, for our changes to take affect, we must overridedatePickerStyle
inside an overridendatePickerDialogTheme
.Here we go:
Override
datePickerDialogTheme
inside your app's base theme:Define
MyDatePickerDialogTheme
. The choice of parent theme will depend on what your app's base theme is: it could be eitherTheme.Material.Dialog
orTheme.Material.Light.Dialog
:We have overridden
datePickerStyle
with the styleMyDatePickerStyle
. The choice of parent will once again depend on what your app's base theme is: eitherWidget.Material.DatePicker
orWidget.Material.Light.DatePicker
. Define it as per your requirements:Currently, we are only overriding
headerBackground
which by default is set to?attr/colorAccent
(this is also why Neil suggestion works in changing the background). But there's quite a lot of customization possible:If you don't want this much control (customization), you don't need to override
datePickerStyle
.colorAccent
controls most of theDatePicker's
colors. So, overriding justcolorAccent
insideMyDatePickerDialogTheme
should work:Overriding
colorAccent
gives you the added benefit of changingOK
&CANCEL
text colors as well. Not bad.This way you don't have to provide any styling information to
DatePickerDialog's
constructor. Everything has been wired properly: